少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
using System;
using System.Collections.Generic;
using UnityEngine;
 
[System.Serializable]
public struct Widget
{
    public string name;
    public GameObject gameObject;
}
 
 
public class WidgetBehavior : MonoBehaviour
{
    [HideInInspector]
    public Widget[] widgets;
    protected Dictionary<string, GameObject> dicWidgts;
 
    private bool hasInitWidgets = false;
 
    /// <summary>
    /// 在子类合适地方初始化此方法
    /// </summary>
    protected void InitWidgts()
    {
        dicWidgts = new Dictionary<string, GameObject>();
        foreach (var item in widgets)
        {
            if (dicWidgts.ContainsKey(item.name))
                DebugEx.LogErrorFormat("有重复的控件名称:{0}", item.name);
            dicWidgts.Add(item.name, item.gameObject);
        }
        hasInitWidgets = true;
    }
 
    public T GetWidgt<T>(string name) where T: Component
    {
        if (!hasInitWidgets)
        {
            DebugEx.LogError("控件还未初始化!");
            return default(T);
        }
        GameObject go;
        if (dicWidgts.TryGetValue(name, out go))
        {
            return go.GetComponent<T>();
        }
        DebugEx.LogErrorFormat("找不到控件:{0}", name);
        return default(T);
    }
 
    public Component GetWidgt(string type, string name)
    {
        if (!hasInitWidgets)
        {
            DebugEx.LogError("控件还未初始化!");
            return null;
        }
        GameObject go;
        if (dicWidgts.TryGetValue(name, out go))
        {
            return go.GetComponent(type);
        }
        DebugEx.LogErrorFormat("找不到控件:{0}", name);
        return null;
    }
 
    public Component GetWidgt(Type type, string name)
    {
        if (!hasInitWidgets)
        {
            DebugEx.LogError("控件还未初始化!");
            return null;
        }
        GameObject go;
        if (dicWidgts.TryGetValue(name, out go))
        {
            return go.GetComponent(type);
        }
        DebugEx.LogErrorFormat("找不到控件:{0}", name);
        return null;
    }
 
}