using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using XFramework; /// /// 创建符合命名规则的脚本 并且进行字段初始化 /// public class ScriptCreater : Editor { /// /// 根据命名习惯和UI框架生成一个Panel的脚本 /// 并遍子物体找到符合命名规则需要控制的组件 声明字段并获取引用 /// Panel命名规则如下:Panel_具体的名字 例如Panel_Loding 生成的类名就是LoadingPanel /// 子物体命名规则如下:组件全拼_名称 例如 Button_close 对应的字段就是 privte Button button_close; /// [MenuItem("Tools/CreatCS/CreatPanelScripts")] public static void CreatPanelCS() { if (Selection.activeGameObject == null|| !Selection.activeGameObject.name.StartsWith("Panel_")) { Debug.LogWarning("需要选择名称为Panel_XX格式的UI创建脚本"); return; } GameObject selectGO = Selection.activeGameObject; if (selectGO.GetComponent()==null) { Debug.LogWarning("需要选择带有RectTransform的UI物体"); return; } string panelName = selectGO.name; string csName = panelName.Split('_')[1].Trim() + "Panel"; string csFoldePath = Application.dataPath + "/Scripts/XAutoCreatCS"; string filePath = $"{csFoldePath}/{csName}.cs"; if (!Directory.Exists(csFoldePath)) Directory.CreateDirectory(csFoldePath); if (File.Exists(filePath)) { Debug.LogWarning("不能创建名称重复的脚本"); return; } StreamWriter sw = new StreamWriter(filePath); sw.WriteLine( "using XFramework;\nusing UnityEngine.UI;" ); sw.WriteLine($"public class {csName} : BasePanel "); sw.WriteLine("{"); sw.WriteLine($"\tconst string panelName = \"{panelName}\";"); Transform [] childTrans = selectGO.transform.GetComponentsInRealChildren();//获取所有未隐藏的子物体 List getReferenceStr = new List();//声明字段时 组织引用获取语句 后续添加进脚本 for (int i = 0; i < childTrans.Length; i++)//梳理需要获取引用的物体 { if (childTrans[i].name.Contains("_"))//带下划线的节点 需要声明字段获取引用 { string fieldType = childTrans[i].name.Split('_')[0]; string fieldName = childTrans[i].name.ToLower(); sw.WriteLine($"\t{fieldType} {fieldName};");//声明一个私有字段 getReferenceStr.Add($"{fieldName}=ActivePanel.FindChildTrans(\"{childTrans[i].name}\").GetComponent<{fieldType}>();");//引用获取语句 } } sw.WriteLine($"\tpublic {csName}() : base(new UIType(panelName))\n\t{{\n\t}}");//构造函数 sw.WriteLine($"\tprotected override void InitEvent()");//重写初始化函数 sw.WriteLine("\t{"); for (int i = 0; i < getReferenceStr.Count; i++) { sw.WriteLine($"\t\t{getReferenceStr[i]}"); } sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Flush(); sw.Close(); AssetDatabase.Refresh(); Debug.Log($"生成UIPanel脚本{csFoldePath}/{csName}"); } /// /// 创建继承Mono的脚本 命名规则同上 /// [MenuItem("Tools/CreatCS/CreatMonoScripts")] public static void CreatMonoCS() { if (Selection.activeGameObject == null) { Debug.LogWarning("需要选择一个物体"); //脚本名称根据物体名称命名 return; } GameObject selectGO = Selection.activeGameObject; if (selectGO.GetComponent() == null) { Debug.LogWarning("需要选择带有Transform的物体"); return; } string panelName = selectGO.name; string csName = panelName.Split('_')[0].Trim() + "Ctrl"; string csFoldePath = Application.dataPath + "/Scripts/XAutoCreatCS"; string filePath = $"{csFoldePath}/{csName}.cs"; if (!Directory.Exists(csFoldePath)) Directory.CreateDirectory(csFoldePath);//创建文件夹 if (File.Exists(filePath)) { Debug.LogWarning("不能创建名称重复的脚本"); return; } StreamWriter sw = new StreamWriter(filePath); sw.WriteLine("using XFramework;\nusing UnityEngine.UI;\nusing UnityEngine;"); sw.WriteLine($"public class {csName} : MonoBehaviour "); sw.WriteLine("{"); Transform[] childTrans = selectGO.transform.GetComponentsInRealChildren();//获取所有未隐藏的子物体 List getReferenceStr = new List();//声明字段时 组织引用获取语句 后续添加进脚本 for (int i = 0; i < childTrans.Length; i++)//梳理需要获取引用的物体 { if (childTrans[i].name.Contains("_"))//带下划线的节点 需要声明字段获取引用 { string fieldType = childTrans[i].name.Split('_')[0]; string fieldName = childTrans[i].name.ToLower(); sw.WriteLine($"\t{fieldType} {fieldName};");//声明一个私有字段 getReferenceStr.Add($"{fieldName}=this.transform.FindChildTrans(\"{childTrans[i].name}\").GetComponent<{fieldType}>();");//引用获取语句 } } sw.WriteLine($"\tvoid Awake()\n\t{{\n\t\tInitEvent();\n\t}}");//Awake 调用脚本初始化函数 sw.WriteLine($"\tvoid InitEvent()");//声明初始化函数 sw.WriteLine("\t{"); for (int i = 0; i < getReferenceStr.Count; i++) { sw.WriteLine($"\t\t{getReferenceStr[i]}"); } sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Flush(); sw.Close(); AssetDatabase.Refresh(); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp"); var type = defaultAssembly.GetType(csName); if (type != null) { selectGO.AddComponent(type);//挂到物体上 return; } Debug.Log($"生成Mono脚本{csFoldePath}/{csName},并挂载到{selectGO.name}"); } /// /// 创建根据物体Trasform初始化的脚本 命名规则同上 /// [MenuItem("Tools/CreatCS/CreatScripts")] public static void GreatCS() { if (Selection.activeGameObject == null) { Debug.LogWarning("需要选择一个物体"); //脚本名称根据物体名称命名 return; } GameObject selectGO = Selection.activeGameObject; if (selectGO.GetComponent() == null) { Debug.LogWarning("需要选择带有Transform的物体"); return; } string panelName = selectGO.name; string csName = panelName.Split('_')[0].Trim(); string csFoldePath = Application.dataPath + "/Scripts/XAutoCreatCS"; string filePath = $"{csFoldePath}/{csName}.cs"; if (!Directory.Exists(csFoldePath)) Directory.CreateDirectory(csFoldePath);//创建文件夹 if (File.Exists(filePath)) { Debug.LogWarning("不能创建名称重复的脚本"); return; } StreamWriter sw = new StreamWriter(filePath); sw.WriteLine("using XFramework;\nusing UnityEngine.UI;\nusing UnityEngine;"); sw.WriteLine($"public class {csName} "); sw.WriteLine("{"); sw.WriteLine($"\tTransform thisTrans;");//声明与脚本对应的Trans字段 Transform[] childTrans = selectGO.transform.GetComponentsInRealChildren();//获取所有未隐藏的子物体 List getReferenceStr = new List();//声明字段时 组织引用获取语句 后续添加进脚本 for (int i = 0; i < childTrans.Length; i++)//梳理需要获取引用的物体 { if (childTrans[i].name.Contains("_"))//带下划线的节点 需要声明字段获取引用 { string fieldType = childTrans[i].name.Split('_')[0]; string fieldName = childTrans[i].name.ToLower(); sw.WriteLine($"\t{fieldType} {fieldName};");//声明一个私有字段 getReferenceStr.Add($"{fieldName}=thisTrans.FindChildTrans(\"{childTrans[i].name}\").GetComponent<{fieldType}>();");//引用获取语句 } } sw.WriteLine($"\tpublic {csName}(Transform thisTrans) \n\t{{\n\t\tthis.thisTrans=thisTrans;\n\t\tInitEvent();\n\t}}");//构造函数 为thisTrans赋值 调用初始化函数 sw.WriteLine($"\tvoid InitEvent()");//声明初始化函数 sw.WriteLine("\t{"); for (int i = 0; i < getReferenceStr.Count; i++) { sw.WriteLine($"\t\t{getReferenceStr[i]}"); } sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Flush(); sw.Close(); AssetDatabase.Refresh(); Debug.Log($"生成物体控制脚本{csFoldePath}/{csName}"); } public static void GreatNetExcuteCS() { } }