UI框架:ui节点控件的绑定方式
<1>思路
1.使用拖拽,生成lua
2.find(path) 麻烦
3.声明就完事了 效率问题
<2>代码
using UnityEngine; using System.Collections.Generic; using System.Reflection; using UnityEngine.UI; public class UIWidget { protected GameObject root; public UIWidget(GameObject root) { this.root = root; init(); } protected virtual void init() { } public virtual void dispose() { } } public class LImage : UIWidget { public LImage(GameObject root) : base(root) { } protected Image _image; protected override void init() { _image = this.root.GetComponent<Image>(); } public void setSprite(string spName) { //this._image = } public void setSprite(Sprite sprite) { this._image.sprite = sprite; } } public class LText : UIWidget { public LText(GameObject root) : base(root) { } protected Text _text; protected override void init() { _text = this.root.GetComponent<Text>(); } public void setText(string str) { this._text.text = str; } } /// <summary> /// 关于UI控件的绑定 /// 1.声明public成员 编辑器拖拽赋值 一般般(很多项目是用的这种方式 动态生成lua代码) /// 2.在初始化函数中 transfrom.find(path) 写path太麻烦 层级更改 path更改 /// 3.只需要声明 比如public string NameText = "Text"; 赋值:uiPool[NameText].setText(string) 需要强转有点烦(lua弱类型可以直接赋值this.NameText) /// 比如 public void enterBtn_click_event(),声明点击函数,ui上的enterBtn自动绑定此方法 /// 效率有点问题 用了反射 节点查找用递归(可以编辑器下生成path 运行时find) /// 但是很方便 只需要声明这些ui控件就行了 /// </summary> public class TRefTest : MonoBehaviour { public string NameText = "Text"; public string IconImg = "Image"; public void enterBtn_click_event() { Debug.LogError("盘子脸"); } private Dictionary<string, UIWidget> uiPool = new Dictionary<string, UIWidget>(); // Use this for initialization void Start() { FieldInfo[] info = this.GetType().GetFields(); for (int i = 0; i < info.Length; i++) { //字段名 string fieldName = info[i].Name; object obj = info[i].GetValue(this); //字段值 string fieldValue = obj.ToString(); //如果是ui组件名称之一 UIWidget uIWidget = createUI(fieldName, fieldValue); uiPool.Add(fieldName, uIWidget); //替换了字段的值 info[i].SetValue(this, fieldName); } //对text设值 LText text = uiPool[NameText] as LText; text.setText("盘子脸"); MethodInfo[] methods = this.GetType().GetMethods(); for (int i = 0; i < methods.Length; i++) { string mName = methods[i].Name; if (mName.IndexOf("_event") != -1) { //bind method bindMethod(mName, methods[i]); } } } void bindMethod(string name, MethodInfo method) { string[] lst = name.Split('_'); GameObject root = this.transform.Find(lst[0]).gameObject; Button btn = root.GetComponent<Button>(); if (btn == null) { btn = root.AddComponent<Button>(); } btn.onClick.AddListener(() => { method.Invoke(this, null); }); } void OnDispose() { foreach (var item in uiPool) { item.Value.dispose(); } } //通过声明的字段名称(即ui节点名称) //和字段值(即ui类型 text image toggle ...) //创建对应的ui控件 public UIWidget createUI(string name, string widget) { //递归查找 GameObject root = this.transform.Find(name).gameObject; UIWidget uIWidget; //通过类型创建widget //反射创建? switch (widget) { case "Text": uIWidget = new LText(root); break; case "Image": uIWidget = new LImage(root); break; default: uIWidget = new UIWidget(root); break; } return uIWidget; } }