这里举两个简单的例子(非原创,不选原创发布不了)

第一个例子是知乎上的例子:

https://zhuanlan.zhihu.com/p/30716595

自动给UI组件绑定

第二个例子是自己的需求,需要根据字符串数组自动生成对应枚举脚本

我只能说例子虽然简单,但是如果再往上拓展,会极大的减少重复性的代码开发操作


文章目录

  • 给Panel添加组件绑定脚本
    • 1.对应素材
    • 2.使用结果(生成脚本)
    • 3.脚本
  • 给定对应字符串数组生成对应枚举
    • 1.使用结果(生成脚本)
    • 2.脚本

给Panel添加组件绑定脚本

1.对应素材

2.使用结果(生成脚本)

3.脚本

using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;public class AutoBuildTemplate
{public static string UIClass =
@"using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
public class #类名# : MonoBehaviour
{
//autopublic void Start(){#查找#}#成员#
}
";
}public class AutoBuild
{[MenuItem("生成/创建或刷新界面")]public static void BuildUIScript(){var dicUIType = new Dictionary<string, string>();dicUIType.Add("Img", "Image");dicUIType.Add("Btn", "Button");dicUIType.Add("Txt", "Text");dicUIType.Add("Tran", "Transform");GameObject[] selectobjs = Selection.gameObjects;foreach (GameObject go in selectobjs){//选择的物体GameObject selectobj = go.transform.root.gameObject;//物体的子物体Transform[] _transforms = selectobj.GetComponentsInChildren<Transform>(true);List<Transform> childList = new List<Transform>(_transforms);//UI需要查询的物体var mainNode = from trans in childList where trans.name.Contains('_') && dicUIType.Keys.Contains(trans.name.Split('_')[0]) select trans;var nodePathList = new Dictionary<string, string>();//循环得到物体路径foreach (Transform node in mainNode){Transform tempNode = node;string nodePath = "/" + tempNode.name;while (tempNode != tempNode.root){tempNode = tempNode.parent;int index = nodePath.IndexOf('/');nodePath = nodePath.Insert(index, "/" + tempNode.name);}nodePathList.Add(node.name, nodePath);}//成员变量字符串string memberstring = "";//查询代码字符串string loadedcontant = "";foreach (Transform itemtran in mainNode){string typeStr = dicUIType[itemtran.name.Split('_')[0]];memberstring += "public " + typeStr + " " + itemtran.name + " = null;\r\n\t";loadedcontant += itemtran.name + " = " + "gameObject.transform.Find(\"" + nodePathList[itemtran.name] + "\").GetComponent<" + typeStr + ">();\r\n\t\t";}string scriptPath = Application.dataPath + "/Scripts/" + selectobj.name + ".cs";string classStr = "";//如果已经存在了脚本,则只替换//auto下方的字符串if (File.Exists(scriptPath)){FileStream classfile = new FileStream(scriptPath, FileMode.Open);StreamReader read = new StreamReader(classfile);classStr = read.ReadToEnd();read.Close();classfile.Close();File.Delete(scriptPath);string splitStr = "//auto";string unchangeStr = Regex.Split(classStr, splitStr, RegexOptions.IgnoreCase)[0];string changeStr = Regex.Split(AutoBuildTemplate.UIClass, splitStr, RegexOptions.IgnoreCase)[1];StringBuilder build = new StringBuilder();build.Append(unchangeStr);build.Append(splitStr);build.Append(changeStr);classStr = build.ToString();}else{classStr = AutoBuildTemplate.UIClass;}classStr = classStr.Replace("#类名#", selectobj.name);classStr = classStr.Replace("#查找#", loadedcontant);classStr = classStr.Replace("#成员#", memberstring);FileStream file = new FileStream(scriptPath, FileMode.CreateNew);StreamWriter fileW = new StreamWriter(file, System.Text.Encoding.UTF8);fileW.Write(classStr);fileW.Flush();fileW.Close();file.Close();Debug.Log("创建脚本 " + Application.dataPath + "/Scripts/" + selectobj.name + ".cs 成功!");AssetDatabase.SaveAssets();AssetDatabase.Refresh();}}}

给定对应字符串数组生成对应枚举

1.使用结果(生成脚本)

2.脚本

using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;public class AutoEnumBuildTemplate
{public static string UIClass =
@"using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
//以下代码都是通过脚本自动生成的
public class #类名#
{#成员#}
";
}public class AutoEnumBuild
{public enum EnumTask{棉花,桃子}[MenuItem("生成/根据字符串生成枚举类型")]public static void BuildUIScript(){string enumSettingName = "AutoCreateEnum";//List<string> strFarmName = new List<string>();strFarmName.Add("苹果");strFarmName.Add("桃子");strFarmName.Add("樱桃");strFarmName.Add("甘蓝");strFarmName.Add("葡萄");//string scriptPath = Application.dataPath + "/Scripts/" + enumSettingName + ".cs";string stringEnumName = "public enum EnumPlant{ ";foreach (var value in strFarmName){stringEnumName += value + ",";}stringEnumName = stringEnumName.Substring(0, stringEnumName.Length - 1);stringEnumName += "}";//string classInfo = AutoEnumBuildTemplate.UIClass;classInfo = classInfo.Replace("#类名#", enumSettingName);classInfo = classInfo.Replace("#成员#", stringEnumName);//FileStream file = new FileStream(scriptPath, FileMode.CreateNew);StreamWriter fileW = new StreamWriter(file, System.Text.Encoding.UTF8);fileW.Write(classInfo);fileW.Flush();fileW.Close();file.Close();//Debug.Log("创建脚本 " + Application.dataPath + "/Scripts/" + enumSettingName+ ".cs 成功!");AssetDatabase.SaveAssets();AssetDatabase.Refresh();}}

Unity代码自动生成相关推荐

  1. 自定义Android注解Part2:代码自动生成

    上一期我们已经把butterknife-annotations中的注解变量都已经定义好了,分别为BindView.OnClick与Keep. 如果你是第一次进入本系列文章,强烈推荐跳到文章末尾查看上篇 ...

  2. 近期活动盘点:高级机器学习训练营、基于神经网络的代码自动生成” “开放学术图谱”、西山金融科技产业创新论坛...

    想知道近期有什么最新活动?大数点为你整理的近期活动信息在此: 清华唐杰教授授课 高级机器学习训练营 我们都知道随着数据复杂度的不断提高,经典机器学习算法已经很难满足实际需求,当前,针对复杂数据对象.复 ...

  3. Mybatis最入门---代码自动生成(generatorConfig.xml配置)

    [一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...

  4. html自动生成工具_关于STM32代码自动生成的工具的进度....

    前情提要:STM32代码自动生成工具_本想...但是...可是...所以 首先说一下那几天大家的反应,有的持观望态度,毕竟STM32CUBE很香:有的很激动,期待我快点出东西:还有的很淡定,知道我在挖 ...

  5. Spring Boot (七)MyBatis代码自动生成和辅助插件

    一.简介 1.1 MyBatis Generator介绍 MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper.dao.entity 的框架 ...

  6. java代码自动生成的插件_如何使用插件生成自定义Java 8代码

    java代码自动生成的插件 大多数程序员讨厌的一件事就是编写样板代码. 无休止的时间花费在设置实体类和配置数据库连接上. 为了避免这种情况,您可以让Speedment Open Source之类的程序 ...

  7. mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap...

    由于mybatis简单易学,比起Hibername来,更容易上手,代码也能自动生成.这几天研究了下代码自动生成的,参考: http://0609xiaohua.iteye.com/blog/14535 ...

  8. 自动产生fsm代码的工具_代码自动生成工具

    构建支持多种数据库类型的代码自动生成工具 背景: 一般的业务代码中写来写去,无外乎是先建好model,然后针对这个model做些CRUD的操作.(主要针对单表的业务操作)针对于数据库dao.mappe ...

  9. 用java写ods系统_基于数据库的代码自动生成工具,生成JavaBean、生成数据库文档、生成前后端代码等(TableGo v7.0.0版)...

    TableGo是基于数据库的代码自动生成工具,低代码编程技术的实现,可以零代码自动生成SpringBoot项目工程.生成JavaBean.生成前后端分离的CRUD代码.生成MyBaits的Mapper ...

最新文章

  1. linux ss 命令简介 端口连接查看 代替netstat
  2. Android运行出现“java.io.IOException: 您的主机中的软件放弃了一个已建立的连接。”...
  3. python numpy np.full(shape, fill_value, dtype=None, order='C')函数 以指定数值填充指定形状的数组
  4. Nacos 2.0 升级前后性能对比压测
  5. 关于JUnit5 你必须知道的(一) JUnit5架构和环境搭建
  6. AcWing 842. 排列数字(DFS)
  7. VS Code 快捷键设置
  8. 编程语言中,取余和取模的区别到底是什么?
  9. ka电器表示什么意思_电气原理中,QS、FU、KM、KA、KI、KT、SB、SQ分别是什么电器元件的文字符号?...
  10. .NET Framework v2.0 Obsolete APIs - 与大家分享
  11. 微信公众平台开发之微信红包的实现
  12. Intro to RL Lecture1
  13. 微信公众号与小程序(十九)
  14. echart vue
  15. 2022年下半年 系统架构师,论文-软件开发模型(Software Development Model)
  16. diskgenius系统迁移_关于系统迁移至SSD
  17. 苯乙酸(PAA)(CAS 103-82-2)的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  18. Incaseformat病毒是如何传播的?我们该如何防护?
  19. pads option选项卡下部分英文释义
  20. early_stopping

热门文章

  1. Ubuntu部署TeamTalk文档
  2. 哈佛结构和冯·诺依曼结构
  3. 通用Redis查询工具类,结合函数编程和设计模式
  4. python入门培训课程
  5. Mule学习-简单示例
  6. win10用户注销后该怎么办?如何彻底删除原有的用户名
  7. Uniapp——拨打电话、发送短信
  8. 十分钟了解K-means聚类
  9. 英特尔的Linux Xe内核图形驱动程序
  10. ViewPager+Fragment