Files
HX_MapEditor/Assets/Scripts/UI/UINpcItem.cs
2025-06-14 13:46:24 +08:00

88 lines
2.5 KiB
C#

using HxGame;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UINpcItem : MonoBehaviour
{
public InputField txtPos; //NPC坐标
public InputField txtID; //NPCID
public Dropdown dropDir; //Npc朝向
public InputField txtPath; //NPC路径
public InputField txtName; //NPC名字
public Button btnDel; //删除
public List<string> paths;
public int itemIdx;
public int pathIdx;
// Start is called before the first frame update
void Awake()
{
AddInputNameClickEvent(txtPos);
AddInputNameClickEvent(txtPath, true);
btnDel.onClick.AddListener(RemoveSelf);
itemIdx = 0;
paths = new List<string>();
}
private void AddInputNameClickEvent(InputField input, bool isPath = false) //可以在Awake中调用
{
var eventTrigger = input.gameObject.AddComponent<EventTrigger>();
UnityAction<BaseEventData> selectEvent = null;
if (isPath)
selectEvent = OnPathInputFieldClicked;
else
selectEvent = OnInputFieldClicked;
EventTrigger.Entry onClick = new EventTrigger.Entry()
{
eventID = EventTriggerType.PointerClick
};
onClick.callback.AddListener(selectEvent);
eventTrigger.triggers.Add(onClick);
}
private void OnInputFieldClicked(BaseEventData data)
{
UIWindow.Instance.uiNpcsPanel.curActiveInput = data.selectedObject.GetComponent<InputField>();
MapManager.Instance.SetCurNpcPointIdx(itemIdx);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.NpcCell);
}
private void OnPathInputFieldClicked(BaseEventData data)
{
UIWindow.Instance.uiNpcsPanel.curActiveInput = data.selectedObject.GetComponent<InputField>();
UIWindow.Instance.uiNpcsPanel.curActiveList = paths;
MapManager.Instance.SetCurNpcPathIdx(pathIdx);
MapManager.Instance.SetEditCellType(MapManager.EditCellType.NpcPath);
}
private void RemoveSelf()
{
DestroyImmediate(gameObject);
MapManager.Instance.RemoveNpcPointSize(itemIdx);
}
public bool CheckValid()
{
if (string.IsNullOrEmpty(txtID.text))
{
UIWindow.Instance.ShowMessage("请填写NpcID");
return false;
}
if (string.IsNullOrEmpty(txtPos.text))
{
UIWindow.Instance.ShowMessage("请填写Npc坐标");
return false;
}
return true;
}
}