unityzgy/Assets/Scripts/ZFramwork/Editor/ScriptCreater.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

219 lines
9.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using XFramework;
/// <summary>
/// 创建符合命名规则的脚本 并且进行字段初始化
/// </summary>
public class ScriptCreater : Editor
{
/// <summary>
/// 根据命名习惯和UI框架生成一个Panel的脚本
/// 并遍子物体找到符合命名规则需要控制的组件 声明字段并获取引用
/// Panel命名规则如下:Panel_具体的名字 例如Panel_Loding 生成的类名就是LoadingPanel
/// 子物体命名规则如下:组件全拼_名称 例如 Button_close 对应的字段就是 privte Button button_close;
/// </summary>
[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<RectTransform>()==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<Transform>();//获取所有未隐藏的子物体
List<string> getReferenceStr = new List<string>();//声明字段时 组织引用获取语句 后续添加进脚本
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}");
}
/// <summary>
/// 创建继承Mono的脚本 命名规则同上
/// </summary>
[MenuItem("Tools/CreatCS/CreatMonoScripts")]
public static void CreatMonoCS()
{
if (Selection.activeGameObject == null)
{
Debug.LogWarning("需要选择一个物体"); //脚本名称根据物体名称命名
return;
}
GameObject selectGO = Selection.activeGameObject;
if (selectGO.GetComponent<Transform>() == 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<Transform>();//获取所有未隐藏的子物体
List<string> getReferenceStr = new List<string>();//声明字段时 组织引用获取语句 后续添加进脚本
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}");
}
/// <summary>
/// 创建根据物体Trasform初始化的脚本 命名规则同上
/// </summary>
[MenuItem("Tools/CreatCS/CreatScripts")]
public static void GreatCS()
{
if (Selection.activeGameObject == null)
{
Debug.LogWarning("需要选择一个物体"); //脚本名称根据物体名称命名
return;
}
GameObject selectGO = Selection.activeGameObject;
if (selectGO.GetComponent<Transform>() == 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<Transform>();//获取所有未隐藏的子物体
List<string> getReferenceStr = new List<string>();//声明字段时 组织引用获取语句 后续添加进脚本
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()
{
}
}