Unity3D中UI组件获取工具

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)
    {

    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值