using HxGame; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; public class UINpcItem : UIBaseItem { 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 paths; public int itemIdx; public int pathIdx; // Start is called before the first frame update protected override void Awake() { base.Awake(); AddInputNameClickEvent(txtPos); AddInputNameClickEvent(txtPath, true); btnDel.onClick.AddListener(RemoveSelf); itemIdx = 0; paths = new List(); } private void AddInputNameClickEvent(InputField input, bool isPath = false) //可以在Awake中调用 { var eventTrigger = input.gameObject.AddComponent(); UnityAction 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(); MapManager.Instance.SetCurNpcPointIdx(itemIdx); MapManager.Instance.SetEditCellType(MapManager.EditCellType.NpcCell); } private void OnPathInputFieldClicked(BaseEventData data) { UIWindow.Instance.uiNpcsPanel.curActiveInput = data.selectedObject.GetComponent(); UIWindow.Instance.uiNpcsPanel.curActiveList = paths; MapManager.Instance.SetEditCellType(MapManager.EditCellType.NpcPath); } public override void SetItemPos(Vector2Int pos) { base.SetItemPos(pos); txtPos.text = pos.ToString(); } private void OnDestroy() { if (sceneArea == null) return; DestroyImmediate(sceneArea.gameObject); } private void RemoveSelf() { DestroyImmediate(gameObject); } 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; } }