using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class UIBase : MonoBehaviour {
    private void Awake()
    {
        Transform[] allChild = transform.GetComponentsInChildren<Transform>();
        for (int i = 0; i < allChild.Length; i++)
        {
            //判断后边的—为_Z的都添加一下UiBase
            if (allChild[i].name.EndsWith("_Z", System.StringComparison.Ordinal))//执行0
            {
                allChild[i].gameObject.AddComponent<UIBase>();
            }
        }
    }
    public GameObject GetGameObject(string widName)
    {
        return UIManager._instance.GetChild(transform.name, widName);
    }
    //这里是But的事件注册
    public void AddButtonLister(string widName, UnityAction action)
    {
        //  GameObject tmpObj = GetGameObject(widName);
        // if (tmpObj!=null)
        //  {
        //    UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
        //      tmpBehav.AddButtonListen(action);
        // }
        UIBehaviours tmpBehav = GetBehaviours(widName);
        if (tmpBehav != null)
        {
            tmpBehav.AddButtonListen(action);
        }
    }
    //这里是Imag的
    public void ChangeImage(string widName, Sprite tmpsprite)
    {
        // GameObject tmpObj = GetGameObject(widName);
        // if (tmpObj != null)
        // {
        //     UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
        //     tmpBehav.ChangeImage(tmpsprite);
        // }
        UIBehaviours tmpBehav = GetBehaviours(widName);
        if (tmpBehav != null)
        {
            tmpBehav.ChangeImage(tmpsprite);
        }
    }
    //简化上面两个方法的重复代码
    public UIBehaviours GetBehaviours(string widName)
    {
        GameObject tmpObj = GetGameObject(widName);
        if (tmpObj != null)
        {
            UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
            return tmpBehav;
        }
        return null;
    }
    }
——————————————————————————————————————————————————————————————————
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour {
public static UIManager _instance;
    Dictionary<string, Dictionary<string, GameObject>> allChild;
    private void Awake()
    {
        _instance = this;
        allChild = new Dictionary<string, Dictionary<string, GameObject>>();//初始化
    }
    /// <summary>
    /// 初始化 添加操作
    /// </summary>
    /// <param name="panleName"></param>
    /// <param name="widName"></param>
    /// <param name="obj"></param>
    public void RegistGameObject(string panleName,string widName,GameObject obj)
    {
        if (!allChild.ContainsKey(panleName))
        {
            allChild[panleName] = new Dictionary<string, GameObject>();
        }
        if (!allChild[panleName].ContainsKey(widName))
        {
            allChild[panleName].Add(widName, obj);
        }
    }
    /// <summary>
    /// 获取的是一个物体
    /// </summary>
    /// <param name="panleName"></param>
    /// <param name="widName"></param>
    /// <returns></returns>
    public GameObject GetChild(string panleName,string widName)
    {
        if (allChild.ContainsKey(panleName))
        {
            return allChild[panleName][widName];
        }
        return null;
    }
}
————————————————————————————————————————————————————————————————————————————————————————
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class UIBehaviours : MonoBehaviour {
    private void Awake()
    {
        //注册到UIManager
        UIBase tempRoot = transform.GetComponentInParent<UIBase>();
        UIManager._instance.RegistGameObject(tempRoot.name, transform.name, gameObject);
    }
    /// <summary>
    /// 注册But事件
    /// </summary>
    /// <param name="action"></param>
    public void AddButtonListen(UnityAction action)
    {
        Button tempBut = GetComponent<Button>();
        if (tempBut!=null)
        {
            tempBut.onClick.AddListener(action);
        }
        
    }
    /// <summary>
    /// 注册Slider事件
    /// </summary>
    /// <param name="action"></param>
    public void AddSliderListen(UnityAction<float> action)
    {
        Slider tempSli = GetComponent<Slider>();
        if (tempSli != null)
        {
            tempSli.onValueChanged.AddListener(action);
        }
    }
    /// <summary>
    /// 注册InputField事件
    /// </summary>
    /// <param name="action"></param>
    public void AddInputFieldListen(UnityAction<string> action)
    {
        InputField tempInp = GetComponent<InputField>();
        if (tempInp != null)
        {
            tempInp.onValueChanged.AddListener(action);
        }
    }
   /// <summary>
   /// Image 加载图片
   /// </summary>
   /// <param name="tmpsprite"></param>
    public void ChangeImage(Sprite tmpsprite)
    {
        Image tempImg =transform.GetComponent<Image>();
        if (tempImg != null)
        {
            tempImg.sprite = tmpsprite;
        }
    }
   /// <summary>
   /// Text 的连接
   /// </summary>
   /// <param name="_tmpText"></param>
    public void ChangeText(string _tmpText)
    {
        Text tempText = transform.GetComponent<Text>();
        if (tempText != null)
        {
            tempText.text = _tmpText;
        }
}
}
————————————————————————————————————————————————————————————————————————
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FriendModle//数据层
{
    public string useName;
public string passWord;
}
public class FriendCtrl : UIBase {//控制层
    FriendModle friendModle;
    FriendLogic friendLogic;
    
    void Start() {
        friendModle = new FriendModle();
        friendLogic = new FriendLogic();
        AddButtonLister("UIBut", friendLogic.OnClick);
        Sprite sprite_ = Resources.Load<Sprite>("SpriteName");
        ChangeImage("UIBut", sprite_);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
public class FriendLogic//逻辑层
{
    public void OnClick()
    {
        Debug.Log("事件");
    }
}
原文:https://www.cnblogs.com/-831/p/12329154.html