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;
|
}
|
|
}
|