| | |
| | | using UnityEngine; |
| | | using UnityEditor; |
| | | using System.Collections.Generic; |
| | | |
| | | public class WindowTool : EditorWindow |
| | | { |
| | | [MenuItem("Tools/窗口管理")] |
| | | private string windowName = ""; |
| | | private int selectedUIIndex = -1; |
| | | private List<string> uiNameList = new List<string>(); |
| | | |
| | | [MenuItem("Tools/WindowTool")] |
| | | public static void ShowWindow() |
| | | { |
| | | EditorWindow.GetWindow<WindowTool>("窗口管理").Show(); |
| | | GetWindow<WindowTool>("WindowTool"); |
| | | } |
| | | |
| | | [SerializeField] |
| | | private string windowName; |
| | | |
| | | private void OnGUI() |
| | | { |
| | | EditorGUILayout.LabelField("窗口名称", EditorStyles.boldLabel); |
| | | |
| | | // 监听TextField变化 |
| | | string newWindowName = EditorGUILayout.TextField("windowName", windowName); |
| | | if (newWindowName != windowName) |
| | | { |
| | | windowName = newWindowName; |
| | | selectedUIIndex = -1; // 手动输入时取消选中 |
| | | } |
| | | |
| | | // 获取UIManager中的界面列表 |
| | | if (UIManager.Instance != null && UIManager.Instance.uiDict != null) |
| | | { |
| | | uiNameList.Clear(); |
| | | foreach (var kv in UIManager.Instance.uiDict) |
| | | { |
| | | uiNameList.Add(kv.Key); |
| | | } |
| | | |
| | | EditorGUILayout.Space(); |
| | | EditorGUILayout.LabelField("UI界面列表", EditorStyles.boldLabel); |
| | | |
| | | int clickedIndex = GUILayout.SelectionGrid(selectedUIIndex, uiNameList.ToArray(), 1); |
| | | |
| | | // 只要点击就覆盖windowName |
| | | if (clickedIndex >= 0 && clickedIndex < uiNameList.Count) |
| | | { |
| | | selectedUIIndex = clickedIndex; |
| | | windowName = uiNameList[selectedUIIndex]; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | EditorGUILayout.HelpBox("UIManager.Instance 或 uiDict 未初始化", MessageType.Warning); |
| | | } |
| | | |
| | | EditorGUILayout.Space(); |
| | | |
| | | // 保留原有的打开和关闭按钮 |
| | | EditorGUILayout.BeginHorizontal(); |
| | | GUILayout.Label("窗口名称"); |
| | | EditorGUILayout.EndHorizontal(); |
| | | windowName = GUILayout.TextField(windowName, GUILayout.MinWidth(300)); |
| | | if (GUILayout.Button("打开")) |
| | | { |
| | | OpenWindow(); |
| | | // 打开窗口逻辑 |
| | | UIManager.Instance.OpenWindow(windowName); |
| | | } |
| | | if (GUILayout.Button("关闭")) |
| | | { |
| | | // 关闭窗口逻辑 |
| | | UIManager.Instance.CloseWindow(windowName); |
| | | } |
| | | EditorGUILayout.EndHorizontal(); |
| | | } |
| | | |
| | | private void OpenWindow() |
| | | { |
| | | if (string.IsNullOrEmpty(windowName) || !Application.isPlaying) |
| | | return; |
| | | UIManager.Instance.OpenWindow(windowName); |
| | | } |
| | | |
| | | } |