using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; /// /// 编辑网格 - 紫色格子 /// 功能:编辑网格, 寻路测试,格子分层显示 /// public class UICellEditor : MonoBehaviour { public Toggle togEdit; public GameObject editPanel; public InputField txtBrushRadius; public Dropdown dropCellType; public Button btnSetBrush; public Button btnSaveCells; /// /// 正在编辑格子 /// public CellType editorGrid { private set; get; } public ToggleGroup editorAreaToggle; public static UICellEditor Instance; private void Awake() { Instance = this; editorGrid = CellType.None; } #region 测试寻路 public Toggle togPathTest; public GameObject pathTestPanel; public InputField txtStartPos; //起点坐标 public InputField txtEndPos; //终点坐标 [HideInInspector] public InputField curActiveInput; #endregion #region 图层编辑 public Toggle togLayer; public GameObject layerPanel; public Toggle[] showgLayers; private int _layers; #endregion public Toggle togEffects; // Start is called before the first frame update void Start() { _layers = 0; editPanel.SetActive(false); pathTestPanel.SetActive(false); layerPanel.SetActive(false); AddInputNameClickEvent(txtStartPos); AddInputNameClickEvent(txtEndPos); } private void OnEnable() { MapManager.Instance.ResetCell(); } private void AddInputNameClickEvent(InputField input) //可以在Awake中调用 { var eventTrigger = input.gameObject.AddComponent(); UnityAction selectEvent = OnInputFieldClicked; EventTrigger.Entry onClick = new EventTrigger.Entry() { eventID = EventTriggerType.PointerClick }; onClick.callback.AddListener(selectEvent); eventTrigger.triggers.Add(onClick); } private void OnInputFieldClicked(BaseEventData data) { if (data.selectedObject.name.Equals("InputStartCell")) { curActiveInput = data.selectedObject.GetComponent(); MapManager.Instance.SetCurPathNodePointIdx(-1); MapManager.Instance.SetEditCellType(MapManager.EditCellType.PathNodeCell); } else if (data.selectedObject.name.Equals("InputEndCell")) { curActiveInput = data.selectedObject.GetComponent(); MapManager.Instance.SetCurPathNodePointIdx(-100); MapManager.Instance.SetEditCellType(MapManager.EditCellType.PathNodeCell); } } public void FindPath() { if(string.IsNullOrEmpty(txtStartPos.text)) { UIWindow.Instance.ShowMessage("起点坐标无效"); return; } string[] tmp = txtStartPos.text.Split(','); if(tmp.Length != 2) { UIWindow.Instance.ShowMessage("起点坐标无效"); return; } if(!APIs.IsUInt(tmp[0])) { UIWindow.Instance.ShowMessage("起点坐标无效"); return; } if (!APIs.IsUInt(tmp[1])) { UIWindow.Instance.ShowMessage("起点坐标无效"); return; } Vector2Int startPos = new Vector2Int(Convert.ToInt32(tmp[0]), Convert.ToInt32(tmp[1])); if (string.IsNullOrEmpty(txtEndPos.text)) { UIWindow.Instance.ShowMessage("终点坐标无效"); return; } tmp = txtEndPos.text.Split(','); if (tmp.Length != 2) { UIWindow.Instance.ShowMessage("终点坐标无效"); return; } if (!APIs.IsUInt(tmp[0])) { UIWindow.Instance.ShowMessage("终点坐标无效"); return; } if (!APIs.IsUInt(tmp[1])) { UIWindow.Instance.ShowMessage("终点坐标无效"); return; } Vector2Int endPos = new Vector2Int(Convert.ToInt32(tmp[0]), Convert.ToInt32(tmp[1])); MapManager.Instance.FindPath(startPos, endPos); } //编辑网格 public void OnEditToggleChange() { if (togEdit.isOn) { if (MapManager.Instance.map == null) { UIWindow.Instance.ShowMessage("请先打开地图"); return; } editPanel.SetActive(true); } else { editPanel.SetActive(false); MapManager.Instance.CloseEditor(); } } //寻路测试 public void OnPathTestToggleChange() { if (togPathTest.isOn) { if (!UIWindow.Instance.uiCellInfo.bMapOpened) { UIWindow.Instance.ShowMessage("请先打开地图"); return; } pathTestPanel.SetActive(true); } else { pathTestPanel.SetActive(false); } } //图层编辑 public void OnLayerToggleChange() { if (togLayer.isOn) { if (!UIWindow.Instance.uiCellInfo.bMapOpened) { UIWindow.Instance.ShowMessage("请先打开地图"); return; } layerPanel.SetActive(true); } else { layerPanel.SetActive(false); MapManager.Instance.ShowCells(); } } //图层编辑 public void OnShowLayerToggleChange() { } //编辑特效 public void OnShowEffectsToggleChange() { MapManager.Instance.HideCells(); } public void EditorAreaToggleChange(Toggle t) { if (t.isOn) { switch (t.name) { case "MoveToggle": editorGrid = CellType.Move; break; case "BlockToggle": editorGrid = CellType.Obstacle; break; case "HideToggle": editorGrid = CellType.Hide; break; case "SafeToggle": editorGrid = CellType.Safe; break; case "StallToggle": editorGrid = CellType.Stall; break; } MapManager.Instance.map?.selector.RefreshPlaneRender(); } if (editorAreaToggle.ActiveToggles().Count() == 0) { Debug.Log("没有选中编辑区域"); editorGrid = CellType.None; } } /// /// 清除所选区域 /// public void CleanSelectArea() { if (editorGrid == CellType.None) { UIWindow.Instance.ShowMessage("当前没有可编辑区域"); return; } if (!MapManager.Instance.isOpenMap()) return; MapManager.Instance.map.selector.ClearSelectArea(); } /// /// 填满所选区域 /// public void FullSelectArea() { if (editorGrid == CellType.None) { UIWindow.Instance.ShowMessage("当前没有可编辑区域"); return; } if (!MapManager.Instance.isOpenMap()) return; MapManager.Instance.map.selector.FullAllArea(); } /// /// 保存地图格子 /// public void SaveObs() { if (!MapManager.Instance.isOpenMap()) return; MapManager.Instance.SaveMapObs(); } private void OnDisable() { foreach(var toggle in editorAreaToggle.ActiveToggles()) { toggle.isOn = false; } } }