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();
}
}
本文仅作为流程分享,具体实现细节还需要根据项目实际情况进行修改和完善。
热门推荐
网上预约办理护照全流程指南
如何评估企业的规模和实力?这类评估有哪些标准?
数据库名字不能带什么字符
在充满变革的世界中生存,企业必须采取的六项韧性战略
S30408不锈钢全面资料介绍
减肥可以吃无花果 无花果吃多了有什么坏处
1952年邱少云牺牲,遗体的一处细节引起重视,三等功被改为特等功
二型糖尿病人能喝牛奶吗
海南万宁兴隆旅游景点大全:精选景区指南
J-20S vs F-22,胜率95%?
老年痴呆怎么查能查出来
王者荣耀地图攻略:三路进击,掌控全局!
抚恤金怎么计算和领
明日方舟新五星干员极境技能详解:先锋爆费位即将补齐
WebGL是什么?探索其在现代网页开发中的应用与潜力
二战结束后,中国军人如何接受日本投降的?在受降仪式上排第几?
商标注册期间该如何使用商标?
昆明至香格里拉旅行:高原反应的可能性与应对策略全解析
为什么说一旦过了60岁后,就要少吃瓜子、核桃?还有老人不听劝
辽宁科技学院怎么样:专业设置与就业前景分析
用什么存钱最安全?怎样选择最安全的存钱方式?
脱发中医辨证如何分型治疗
哺乳期十大最佳零食
无锡大运河畔的新生:北塘大街段滨水空间改造记
研究发现:未来30年心血管疾病将大幅增加,三大风险因素需警惕
唐代阙楼的等级之巅——凌烟阁
AI时代,如何做不可替代的智慧教师?
历史假设下的思索:如果刘备统一天下
如何处理机动车与行人发生交通事故的责任划分
通货紧缩对老百姓意味着什么?通货紧缩对老百姓生活的实际影响