1,UIBase腳本:向Ui的功能鍵(經過功能鍵名字末尾或者開頭判斷「-」)添加腳本UiMangager(工具類),UIBahav的方法封裝進Base按鈕註冊事件方法。ide
2,UIManager腳本:寫個容器,單例,初始化操做,獲取一個物體工具
3,UIBahavious腳本:註冊進UIManager,寫註冊按鈕事件方法。this
4,FriendCtrl腳本:裏面有數據層,控制層(繼承UIBase),邏輯層(按鈕事件的具體方法)。spa
UIBase:orm
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;
}
}圖片
——————————————————————————————————————————————————————————————————string
UIManagerit
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;
}
}
———————————————————————————————————————————————————————————————————————————————
UIBehaviours
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("事件");
}
}