Unity UGUI一键绑定UI控件工具开发指南:基于反射特性的优化方案
创作时间:
作者:
@小白创作中心
Unity UGUI一键绑定UI控件工具开发指南:基于反射特性的优化方案
引用
CSDN
1.
https://blog.csdn.net/qq_54351806/article/details/140037013
在Unity开发中,UI控件的绑定是一个常见且繁琐的任务。传统的Transform.Find方法不仅步骤复杂,而且占用空间大,修改起来也不方便。本文将介绍一种基于反射特性的优化方案,通过自定义特性来简化UI控件的绑定过程,实现一键绑定,从而大大加快开发速度。
效果
通过本方案,一行代码即可完成UI控件的绑定。配合后续的生成绑定工具,可以进一步提升开发效率。
实现过程
首先准备面板基类和面板管理类,并创建一个用来测试的面板。
自定义特性
自定义一个特性UIBinderAttribute
,用于标记需要被查找的组件。特性中包含一个path
字段,用于存储组件的地址。
面板基类中的组件绑定
在面板基类中实现组件绑定逻辑:
var thisType = this.GetType(); // 获取当前面板的类型
var thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); // 获取当前面板的所有字段
Transform trans;
foreach (var field in thisFields)
{
var atb = field.GetCustomAttribute(typeof(UIBinderAttribute), false) as UIBinderAttribute;
trans = this.transform.Find(atb.path);
field.SetValue(this, trans.GetComponent(field.FieldType));
}
优化路径生成
为了进一步优化开发体验,我们开发了一个编辑器工具,可以自动生成绑定代码:
点击工具后,会自动将选中UI组件的绑定代码复制到剪贴板,方便直接粘贴使用。
源代码
以下是完整的源代码实现:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
public class ABaseUI : MonoBehaviour
{
public void Init()
{
BindingUIComponent();
}
private void BindingUIComponent()
{
var thisType = this.GetType();
var thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
UIBinderAttribute atb;
Transform trans;
foreach (var field in thisFields)
{
atb = field.GetCustomAttribute(typeof(UIBinderAttribute), false) as UIBinderAttribute;
trans = this.transform.Find(atb.path);
field.SetValue(this, trans.GetComponent(field.FieldType));
Debug.Log(field.FieldType);
Debug.Log(field.GetType());
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.UI;
using System.Text;
using TMPro;
using UnityEngine.EventSystems;
using Random = System.Random;
public class UIBinderTool : MonoBehaviour
{
[MenuItem("GameObject/UIBindingTool", false, 1)]
private static void SingleReplace()
{
if (Selection.gameObjects.Length < 1)
return;
GameObject selectObj = Selection.gameObjects[0];
UIBehaviour[] componments = selectObj.GetComponents<UIBehaviour>();
Type tartgetType = null;
foreach (var componment in componments)
{
if (componments.Length > 1)
{
if (componment.GetType() is Image)
continue;
}
tartgetType = componment.GetType();
}
StringBuilder pasteContent = new StringBuilder();
Transform trans = selectObj.transform;
string pathContent = string.Empty;
while (!trans.name.Equals("root"))
{
pathContent = trans.parent.name + "/" + pathContent;
trans = trans.parent;
}
pasteContent.Append("\t" + $"[UIBinder(\"{pathContent.ToString()}{selectObj.name}\")]" + "private" + " " + tartgetType.Name + " a;");
TextEditor text = new TextEditor();
text.text = pasteContent.ToString();
text.SelectAll();
text.Copy();
Debug.Log("生成成功");
}
}
public class UIBinderAttribute : Attribute
{
public string path;
public UIBinderAttribute(string path)
{
this.path = path;
}
}
public class UIManager : MonoBehaviour
{
void Start()
{
var canvas = GameObject.Find("Canvas").transform;
var panel = Resources.Load<GameObject>("LoadingPanel");
var panelObj = GameObject.Instantiate(panel, canvas);
panelObj.GetOrAddComponent<LoadingPanel>().Init();
}
}
本文仅作为流程分享,具体实现细节还需要根据项目实际情况进行修改和完善。
热门推荐
北京人最爱的羊蝎子家常做法
一文掌握数据库测试:四大维度保障数据安全与性能
上海到恩施:5天1000元旅游攻略,含省钱购票方案
上海到恩施无直达列车,汉口南京南两大中转方案
权威发布:一级消防工程师报考条件及备考指南
别盲目追捧3.5分餐厅:差评君带你理性看待餐厅评分
跟着燕子导游玩转潮州古城:两天一夜精华游
辅酶Q10:抑郁症治疗的新希望
新冠康复期,辅酶Q10守护心血管健康
古天乐捐建135所希望小学,4000万善款播撒教育火种
王羲之兰亭序:自然、生命与虚无的书法表达
《兰亭序》:王羲之的书法巅峰与人生感悟
兰亭景区:书法圣地焕发新生,从千年古迹到文化创新
皮肤肿块莫轻视:脂肪瘤囊肿的诊断与治疗指南
一文读懂脂肪瘤和囊肿:从症状到治疗全解析
网球肘康复全攻略:从物理治疗到日常护理
香菜不止是调味:秋冬季节的养生食材搭配指南
香菜:营养超柠檬番茄,8大功效但非人人可食
巨细胞病毒肺炎:最新研究进展
李嘉欣投身教育公益:惠灵顿筹款到贫困生资助
香菜牛肉烹饪指南:从食材选购到创新搭配
李嘉诚大动作!7.6折甩卖北京一项目,房价较地价猛涨50倍
手指甲劈裂是怎么回事 八种方法保护你的美甲
双十一剁手党必备:移动支付让你秒变购物达人
喝红茶真的能拯救你的心血管?
红茶中的抗氧化成分真的这么牛?
夏季钓鲢鳙,这些技巧让你爆护满满
冬季新手钓鲢鳙实用指南
阿斯美治疗哮喘效果显著,四类人群禁用需警惕
算法推荐系统深刻改变信息分发逻辑,重塑消费体验与内容生态