三国卡牌客户端基础资源仓库
yyl
5 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
 
public class UIAdjustEditor : EditorWindow
{
    private string uiName = "";
    private bool destroyOnClose = false;
    private Vector2 scrollPos;
 
    [MenuItem("Tools/UI 调试工具")]
    public static void ShowWindow()
    {
        GetWindow<UIAdjustEditor>("UI 调试工具");
    }
 
    void OnGUI()
    {
        EditorGUILayout.LabelField("UI 调试工具", EditorStyles.boldLabel);
        EditorGUILayout.Space();
 
        uiName = EditorGUILayout.TextField("界面名字", uiName);
        destroyOnClose = EditorGUILayout.Toggle("关闭时销毁", destroyOnClose);
 
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("打开 UI"))
        {
            if (Application.isPlaying && !string.IsNullOrEmpty(uiName))
            {
                UIManager.Instance.OpenWindow(uiName);
            }
            else
            {
                Debug.LogWarning("请在运行时操作,并输入界面名字。");
            }
        }
        if (GUILayout.Button("关闭 UI"))
        {
            if (Application.isPlaying && !string.IsNullOrEmpty(uiName))
            {
                UIManager.Instance.CloseWindow(uiName, destroyOnClose);
            }
            else
            {
                Debug.LogWarning("请在运行时操作,并输入界面名字。");
            }
        }
        EditorGUILayout.EndHorizontal();
 
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("当前已打开的界面:", EditorStyles.boldLabel);
 
        if (Application.isPlaying)
        {
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(200));
            var dict = GetOpenUIDict();
            if (dict.Count == 0)
            {
                EditorGUILayout.LabelField("无已打开的界面");
            }
            else
            {
                foreach (var kv in dict)
                {
                    EditorGUILayout.BeginHorizontal();
                    // 名字做成按钮
                    if (GUILayout.Button($"{kv.Key}  (数量: {kv.Value})", GUILayout.Width(200)))
                    {
                        // 通过反射获取UI GameObject
                        var mgr = UIManager.Instance;
                        var field = typeof(UIManager).GetField("uiDict", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        if (field != null)
                        {
                            var uiDict = field.GetValue(mgr) as Dictionary<string, List<UIBase>>;
                            if (uiDict != null && uiDict.TryGetValue(kv.Key, out var list) && list != null && list.Count > 0)
                            {
                                var ui = list[0];
                                if (ui != null && ui.gameObject != null)
                                {
                                    Selection.activeObject = ui.gameObject;
                                    EditorGUIUtility.PingObject(ui.gameObject);
                                }
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            EditorGUILayout.EndScrollView();
        }
        else
        {
            EditorGUILayout.HelpBox("请在运行时查看已打开的界面。", MessageType.Info);
        }
    }
 
    /// <summary>
    /// 获取当前已打开的UI名称及数量
    /// </summary>
    private Dictionary<string, int> GetOpenUIDict()
    {
        var result = new Dictionary<string, int>();
        var mgr = UIManager.Instance;
        var field = typeof(UIManager).GetField("uiDict", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        if (field != null)
        {
            var dict = field.GetValue(mgr) as Dictionary<string, List<UIBase>>;
            if (dict != null)
            {
                foreach (var kv in dict)
                {
                    if (kv.Value != null && kv.Value.Count > 0)
                        result[kv.Key] = kv.Value.Count;
                }
            }
        }
        return result;
    }
}