这里举两个简单的例子(非原创,不选原创发布不了)
第一个例子是知乎上的例子:
https://zhuanlan.zhihu.com/p/30716595
自动给UI组件绑定
第二个例子是自己的需求,需要根据字符串数组自动生成对应枚举脚本
我只能说例子虽然简单,但是如果再往上拓展,会极大的减少重复性的代码开发操作
给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
{
//auto
public 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();
}
}
|