一:前言

对于面板赋值或Find绑定UI组件,我们可以使用一种工具化的方式去自动生成代码并绑定对象,增加效率
分为logic和view,view层是UI界面上的组件,每次都会自动生成并覆盖,logic层是逻辑


二:使用


例如一个UI界面,我们只需要做成预制体并在Project下右键预制体,选择AutoGen/Create View则会自动生成view和logic两个脚本,logic是我们要编写逻辑的脚本,view是每次都会自动生成并覆盖的脚本


三:说明

——以下几个路径都是可以自定义的(view和logic生成的路径、view和logic模版文件的路径)

——可以自定义忽略的组件类型列表

——只会生成对象名带下划线的


四:代码实现

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
using System;public class AutoGenCode
{//logic层代码路径const string LogicDir = "Assets/AutoGen/Logic";//view层代码路径const string ViewDir = "Assets/AutoGen/View";//logic层模版文件路径const string LogicTempletePath = "Assets/AutoGen/LogicTemplete.txt";//view层模版文件路径const string ViewTempletePath = "Assets/AutoGen/ViewTemplete.txt";//命名空间模板const string NameSpaceTemplete = "using {0};";//字段模板const string FieldTemplete = "public {0} {1};\t";//方法模板const string MethodTemplete = "{0} = gameObject.transform.Find(\"{1}\").GetComponent<{2}>();\t\t";/// <summary>/// 忽略的组件类型列表/// </summary>static List<Type> IgnoreComponentTypeList = new List<Type>(){typeof(CanvasRenderer),typeof(RectTransform),};[MenuItem("Assets/AutoGen/Create View", priority = 0)]static void CreateLogicAndView(){GameObject go = Selection.activeGameObject;//判断是否是prefabif (PrefabUtility.GetPrefabAssetType(go) != PrefabAssetType.Regular){Debug.LogWarning("选择的不是预制体,选择的对象:" + go.name);return;}if (!Directory.Exists(ViewDir)){Directory.CreateDirectory(ViewDir);}if (!Directory.Exists(LogicDir)){Directory.CreateDirectory(LogicDir);}string className = go.name + "View";StringBuilder fieldContent = new StringBuilder();StringBuilder methodContent = new StringBuilder();StringBuilder nameSpaceContent = new StringBuilder();nameSpaceContent.AppendLine(NameSpaceTemplete.Replace("{0}", "UnityEngine"));//必须有UnityEngine命名空间string logicTempleteContent = File.ReadAllText(LogicTempletePath, Encoding.UTF8);string viewTempleteContent = File.ReadAllText(ViewTempletePath, Encoding.UTF8);string logicPath = LogicDir + "/" + go.name + "Logic.cs";string viewPath = ViewDir + "/" + go.name + "View.cs";List<string> tempNameSpaceList = new List<string>();//计算所有子物体组件数据List<ComponentInfo> infoList = new List<ComponentInfo>();CalcComponentInfo("", go.transform, infoList);foreach (var tempInfo in infoList){//字段string tempFieldStr = FieldTemplete.Replace("{0}", tempInfo.TypeStr);tempFieldStr = tempFieldStr.Replace("{1}", tempInfo.FieldName);fieldContent.AppendLine(tempFieldStr);//绑定方法string tempMethodStr = MethodTemplete.Replace("{0}", tempInfo.FieldName);tempMethodStr = tempMethodStr.Replace("{1}", tempInfo.Path);tempMethodStr = tempMethodStr.Replace("{2}", tempInfo.TypeStr);methodContent.AppendLine(tempMethodStr);//命名空间if (!tempNameSpaceList.Contains(tempInfo.NameSpace)){string tempNameSpaceStr = NameSpaceTemplete.Replace("{0}", tempInfo.NameSpace);tempNameSpaceList.Add(tempInfo.NameSpace);nameSpaceContent.AppendLine(tempNameSpaceStr);}}//logic层脚本if (!File.Exists(logicPath)){using (StreamWriter sw = new StreamWriter(logicPath)){string content = logicTempleteContent;content = content.Replace("#CLASSNAME#", className);sw.Write(content);sw.Close();}}//view层脚本using (StreamWriter sw = new StreamWriter(viewPath)){string content = viewTempleteContent;content = content.Replace("#NAMESPACE#", nameSpaceContent.ToString());content = content.Replace("#CLASSNAME#", className);content = content.Replace("#FIELD_BIND#", fieldContent.ToString());content = content.Replace("#METHOD_BIND#", methodContent.ToString());sw.Write(content);sw.Close();}AssetDatabase.Refresh();}/// <summary>/// 计算所有子物体组件数据/// </summary>static void CalcComponentInfo(string path, Transform child, List<ComponentInfo> infoList){bool isRoot = string.IsNullOrEmpty(path);if (!isRoot&& IsVaildField(child.name)){var componentList = child.GetComponents<Component>();foreach (var tempComponent in componentList){ComponentInfo info = new ComponentInfo(){Path = path,go = child.gameObject,NameSpace = tempComponent.GetType().Namespace,TypeStr = tempComponent.GetType().Name,};if (!HaveSameComponentInfo(info, infoList)&& !IgnoreComponentTypeList.Contains(tempComponent.GetType())){infoList.Add(info);}}}foreach (Transform tempTrans in child.transform){CalcComponentInfo(isRoot ? tempTrans.name : path + "/" + tempTrans.name, tempTrans.transform, infoList);}}/// <summary>/// 是否为合法的字段名/// </summary>static bool IsVaildField(string goName){if (goName.Contains("_")){if (int.TryParse(goName[0].ToString(), out _)){Debug.LogWarning("字段名不能以数字开头:, goName :" + goName);return false;}return true;}return false;}/// <summary>/// 是否有相同的组件数据/// </summary>static bool HaveSameComponentInfo(ComponentInfo info, List<ComponentInfo> infoList){foreach (var tempInfo in infoList){if (tempInfo.FieldName == info.FieldName){Debug.LogWarning("子物体名重复:, goName :" + info.go.name);return true;}}return false;}
}/// <summary>
/// 组件数据
/// </summary>
public class ComponentInfo
{public string Path;public GameObject go;public string NameSpace;public string TypeStr;public string FieldName{get{return $"{go.name}_{TypeStr}";}}
}

Unity编辑器扩展——自动生成UI界面脚本相关推荐

  1. Unity编辑器扩展批量生成游戏子物体

    要求选中层级视图中的gameobject,然后从resources文件夹里实例化预制体. using System.Collections; using System.Collections.Gene ...

  2. 【Unity编辑器扩展】(三)PSD转UGUI Prefab, 一键拼UI解放美术/程序(完结)

    工具效果: 第一步,把psd图层转换为可编辑的节点树,并自动解析UI类型.自动绑定UI子元素: 第二步, 点击"生成UIForm"按钮生成UI预制体 (若有UI类型遗漏可在下拉菜单 ...

  3. Unity编辑器扩展 UI控件篇

    前摇 :认识编辑器扩展的必要性 由于各种各样的原因,无论是移动端亦或是主机/PC端,进几年的发行的游戏体量是越来越大.通常来说大体量的游戏开发需要一套很成熟完善的工作流,亦或说有很强的工业化的能力,像 ...

  4. 【Unity编辑器扩展】(二)PSD转UGUI Prefab, 图层解析和碎图导出

    书接上回:[Unity编辑器扩展](一)PSD转UGUI Prefab, Aspose.PSD和Harmony库的使用_TopGames的博客-CSDN博客 工具使用预览: 工具目标: 1. 实现将p ...

  5. 【Unity编辑器扩展】(一)PSD转UGUI Prefab, Aspose.PSD和Harmony库的使用

    [Unity编辑器扩展](二)PSD转UGUI Prefab, 图层解析和碎图导出_psd导入unity_TopGames的博客-CSDN博客 [Unity编辑器扩展](三)PSD转UGUI Pref ...

  6. Unity编辑器扩展: 程序化打图集工具

    开始前的声明:该案例中图集所使用图片资源均来源于网络,仅限于学习使用 一.前言 关于编辑器扩展相关的知识,在前面的两篇内容中做了详细的描述,链接地址: 第一篇 :Unity编辑器扩展 UI控件篇 第二 ...

  7. 根据Word表格自动生成SQL数据库脚本的VBScript代码

    这是几年前写的根据Word表格自动生成SQL数据库脚本的VBScript代码,最近修改了下(原来只支持单个Word表格)使其支持一个Word文档中的多个表格,生成的SQL文件名以Word文件名+.SQ ...

  8. Unity编辑器扩展之EditorWindow

    Unity编辑器扩展之EditorWindow 继承这个类的编辑器脚本可以用来创建一个编辑器窗口,类似Inspector窗口 若要在这个类中添加一些控件,可以使用GUI和GUILayout控件,还可以 ...

  9. Unity 编辑器扩展菜单

    Unity 编辑器扩展菜单 目录 Unity 编辑器扩展菜单 一.MenuItem 特性 菜单栏扩展 1.创建多级菜单 2.创建可使用快捷键的菜单项 3.创建可被勾选的菜单项 4.检查菜单是否使用 5 ...

最新文章

  1. Java Day02-2(字符串)
  2. js选择checkbox值,组织成key-value形式,传值到后台
  3. 【EventBus】EventBus 源码解析 ( 事件发送 | postToSubscription 方法 | EventBus 线程模式处理细节 )
  4. mysql Communications link failure druid
  5. SAP CRM WebClient UI的cancel按钮处理
  6. PAT_B_1007_Java(20分)
  7. 阿里合伙人程立:阿里15年,我撕掉了身上两个标签
  8. Python PCA降维小例子
  9. UVa 706 ZOJ 1146 LC-Display
  10. BZOJ 3329 Xorequ 数字DP+矩阵乘法
  11. java 16进制Util转换类
  12. 兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP
  13. 街头篮球服务器未响应,我的生涯我做主 《街头篮球》生涯联赛FAQ
  14. 熔断机制什么意思_熔断机制是什么意思
  15. Python判断一个词语是不是人名
  16. 计算机硬件知识总结,计算机硬件知识总结(二)
  17. 从小数据量 MySQL 迁移数据到 TiDB
  18. 君澜酒店集团2019年度待开业“景澜”品牌酒店发布
  19. 这种 Github 不要写在简历上!你们有没有写过?
  20. 关于 Microsoft Visual Studio

热门文章

  1. 火车头抓取豆瓣影评案例
  2. HGame 2023 Week4 部分Writeup
  3. 网站备案和公安备案区别
  4. 【无标题】win11安装Oracle 12c [INS-32102] 指定的 Oracle 主目录用户已存在
  5. 航天安网高清视频无损压缩解决方案—IDC机房监控系统案例
  6. 儿童护眼台灯哪个品牌比较好?四款质量非常好的护眼台灯推荐
  7. python3中文编码转换显示
  8. 关于Spring boot中的Quartz配置启动开关问题(启动、停止)
  9. 科学计算机计算坐标编程,Calculator科学计算器的教程
  10. npm包管理工具与ES6官方模块化规范