using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ComponentFindTool
{
private Dictionary<string, GameObject> childGameObjects = new Dictionary<string, GameObject>();
public static ComponentFindTool CreateInstance(GameObject root)
{
return new ComponentFindTool(root);
}
private ComponentFindTool(GameObject root)
{
Init(root);
}
public void Init(GameObject root)
{
Transform[] trans = root.GetComponentsInChildren<Transform>(true);
for (int i = 0; i < trans.Length; i++)
{
Transform item = trans[i];
if (!childGameObjects.ContainsKey(item.name.ToLower()))
{
childGameObjects.Add(item.name.ToLower(), item.gameObject);
}
else
{
Debug.LogError("please rename : " + item.name);
}
}
}
public GameObject GetGameObject(string objName)
{
GameObject obj = null;
if (!string.IsNullOrEmpty(objName))
{
childGameObjects.TryGetValue(objName.ToLower(), out obj);
}
return obj;
}
public T GetComp<T>(string componentName) where T : Component
{
if (!string.IsNullOrEmpty(componentName))
{
GameObject obj = GetGameObject(componentName);
if (obj != null)
{
return obj.GetComponent<T>();
}
}
return null;
}
}
public class UIBase : MonoBehaviour
{
private ComponentFindTool findTool;
protected ComponentFindTool FindTool
{
get
{
if (findTool == null)
{
findTool = ComponentFindTool.CreateInstance(gameObject);
}
return findTool;
}
}
protected GameObject GetGameObject(string objName)
{
if (string.IsNullOrEmpty(objName))
{
return null;
}
return FindTool.GetGameObject(objName);
}
protected T GetComp<T>(string componentName) where T : Component
{
if (string.IsNullOrEmpty(componentName))
{
return null;
}
return FindTool.GetComp<T>(componentName);
}
public virtual void InitUI()
{
}
/// <summary>
/// 持有视图层对象的中介者可以通过这个方法注册按钮点击事件
/// </summary>
/// <param name="target">中介者</param>
/// <param name="btnNames">所有的按钮名称,对应UI控件</param>
/// <param name="prefix">方法的前缀</param>
public void BindButtons(object target, List<string> btnNames, string prefix = "OnClick")
{
Type targetType = target.GetType();
for (int i = 0; i < btnNames.Count; i++)
{
GameObject go = FindTool.GetGameObject(btnNames[i]);
if (go == null)
{
Debug.LogErrorFormat("Bind Btn Faild -->FindTool.GetGameObject {0}", btnNames[i]);
continue;
}
string methodName = prefix + btnNames[i];
//--
if (targetType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly) == null)
{
Debug.LogErrorFormat("Bind Btn Faild -->targetType.GetMethod {0}", btnNames[i]);
continue;
}
Action<PointerEventData> fuc = (Action<PointerEventData>)Delegate.CreateDelegate(typeof(Action<PointerEventData>), target, methodName, true, false);
if (fuc == null)
{
Debug.LogErrorFormat("Bind Btn Faild -->Delegate.CreateDelegate {0}", btnNames[i]);
continue;
}
//--
--.NET 4.x
//MethodInfo methodInfo = targetType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
//if (methodInfo == null)
//{
// Debug.LogErrorFormat("Bind Btn Faild -->targetType.GetMethod {0}", btnNames[i]);
// continue;
//}
//Action<PointerEventData> fuc1 = (Action<PointerEventData>)methodInfo.CreateDelegate(typeof(Action<PointerEventData>), target);
//if (fuc1 == null)
//{
// Debug.LogErrorFormat("Bind Btn Faild -->methodInfo.CreateDelegate {0}", btnNames[i]);
// continue;
//}
--
UIEvent.Get(go).OnPointerClickCallBack = fuc;
}
}
}
public class ClientUI : UIBase
{
private Text myText;
public override void InitUI()
{
myText = GetComp<Text>("myText");
}
}
public class ClientMediator
{
private ClientUI clientUI;
public ClientMediator(UIBase uIBase)
{
clientUI = (ClientUI)uIBase;
clientUI.InitUI();
clientUI.BindButtons(this, new List<string>()
{
"CloseBtn",
"StartBtn"
});
}
private void OnClickCloseBtn(PointerEventData e)
{
}
private void OnClickStartBtn(PointerEventData e)
{
}
}