From 6cfae215dc9294a9dd42fcad5d198225907227e1 Mon Sep 17 00:00:00 2001 From: EdwardLee <535889004@qq.com> Date: Fri, 21 Jun 2024 00:59:26 +0800 Subject: [PATCH] 11 --- .../TEngine/Editor/UI/ScriptGenerator.cs | 2 +- .../Assets/TEngine/Editor/UI/TEUIHelper.cs | 73 +++++++++ .../TEngine/Editor/UI/TEUIHelper.cs.meta | 11 ++ .../TEngine/Editor/UI/TEUIHelperWindiw.cs | 138 ++++++++++++++++++ .../Editor/UI/TEUIHelperWindiw.cs.meta | 11 ++ 5 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs create mode 100644 UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs.meta create mode 100644 UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs create mode 100644 UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs.meta diff --git a/UnityProject/Assets/TEngine/Editor/UI/ScriptGenerator.cs b/UnityProject/Assets/TEngine/Editor/UI/ScriptGenerator.cs index 9cc62fbe..e9d6973e 100644 --- a/UnityProject/Assets/TEngine/Editor/UI/ScriptGenerator.cs +++ b/UnityProject/Assets/TEngine/Editor/UI/ScriptGenerator.cs @@ -94,7 +94,7 @@ namespace TEngine.Editor.UI } } - private static void Ergodic(Transform root, Transform transform, ref StringBuilder strVar, ref StringBuilder strBind, ref StringBuilder strOnCreate, + public static void Ergodic(Transform root, Transform transform, ref StringBuilder strVar, ref StringBuilder strBind, ref StringBuilder strOnCreate, ref StringBuilder strCallback, bool isUniTask) { for (int i = 0; i < transform.childCount; ++i) diff --git a/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs new file mode 100644 index 00000000..57e8fd9b --- /dev/null +++ b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs @@ -0,0 +1,73 @@ +using System.Collections; +using System.Collections.Generic; +using System.Text; +using UnityEngine; +#region +//作者:Saber +#endregion +namespace TEngine.Editor.UI +{ + public class TEUIHelper + { + + public static string Generate(bool includeListener, bool isUniTask = false, Transform root = null, string nameSpace = "",string className="") + { + if (root != null) + { + StringBuilder strVar = new StringBuilder(); + StringBuilder strBind = new StringBuilder(); + StringBuilder strOnCreate = new StringBuilder(); + StringBuilder strCallback = new StringBuilder(); + + ScriptGenerator.Ergodic(root, root, ref strVar, ref strBind, ref strOnCreate, ref strCallback, isUniTask); + + //object[] args = new object[] { root, root, strVar, strBind, strOnCreate, strCallback, isUniTask }; + //typeof(TEngine.Editor.UI.ScriptGenerator).GetMethod("Ergodic",System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Static).Invoke(null,args); + //strVar = args[2] as StringBuilder; + //strBind = args[3] as StringBuilder; + //strOnCreate = args[4] as StringBuilder; + //strCallback = args[5] as StringBuilder; + + StringBuilder strFile = new StringBuilder(); + + if (includeListener) + { +#if ENABLE_TEXTMESHPRO + strFile.Append("using TMPro;\n"); +#endif + if (isUniTask) + { + strFile.Append("using Cysharp.Threading.Tasks;\n"); + } + } + strFile.Append("using UnityEngine;\n"); + strFile.Append("using UnityEngine.UI;\n"); + strFile.Append("using TEngine;\n\n"); + nameSpace = string.IsNullOrEmpty(nameSpace) ? SettingsUtils.GetUINameSpace() : nameSpace; + strFile.Append($"namespace {nameSpace}\n"); + strFile.Append("{\n"); + //strFile.Append("\t[Window(UILayer.UI)]\n"); + strFile.Append("\tpartial class " + className + "\n"); + strFile.Append("\t{\n"); + + // 脚本工具生成的代码 + strFile.Append("\t\t#region 脚本工具生成的代码\n"); + strFile.Append(strVar); + strFile.Append("\t\tprotected override void ScriptGenerator()\n"); + strFile.Append("\t\t{\n"); + strFile.Append(strBind); + strFile.Append(strOnCreate); + strFile.Append("\t\t}\n"); + strFile.Append("\t\t#endregion"); + + strFile.Append("\n\t}"); + strFile.Append("\n}"); + return strFile.ToString(); + + } + return string.Empty; + } + + } +} + diff --git a/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs.meta b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs.meta new file mode 100644 index 00000000..23acdd8a --- /dev/null +++ b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 783105f2b5405eb45bb29a4f5131dc3e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs new file mode 100644 index 00000000..806b04ee --- /dev/null +++ b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs @@ -0,0 +1,138 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Sirenix.OdinInspector; +using Sirenix.OdinInspector.Editor; +using System.IO; +using UnityEditor; +using UnityEditor.SceneManagement; +#region +//作者:Saber +#endregion +namespace TEngine.Editor.UI +{ + + public class TEUIToolWindow : OdinEditorWindow + { + public enum Type + { + normal, + unitask, + listener, + all + } + [ReadOnly, LabelText("选中物体")] + public Transform root; + [LabelText("生成类型")] + public Type type; + [LabelText("命名空间")] + public string nameSpace = ""; + [LabelText("类名")] + public string className; + [LabelText("生成位置"), FolderPath] + public string savePath= "Assets/Scripts/UIScriptsAuto"; + + [ReadOnly, LabelText("生成的脚本"),HorizontalGroup(GroupID ="BS")] + public TextAsset buildScript; + + protected override void OnDisable() + { + base.OnDisable(); + Save(); + + } + void Save() + { + EditorPrefs.SetString($"Last_UIScriptsAutoNameSpace", nameSpace); + EditorPrefs.SetInt($"Last_UIScriptsAutoType", (int)type); + EditorPrefs.SetString($"Last_UIScriptsAutoSavePath", savePath); + } + void Load() + { + nameSpace = EditorPrefs.GetString($"Last_UIScriptsAutoNameSpace", nameSpace); + type = (Type)EditorPrefs.GetInt($"Last_UIScriptsAutoType", (int)type); + var newSavePath = EditorPrefs.GetString($"Last_UIScriptsAutoSavePath", savePath); + if (string.IsNullOrEmpty(newSavePath)==false) + { + savePath = newSavePath; + OnSavePathChange(); + } + } + void OnSavePathChange() + { + var originPath = $"{savePath}/{className}_Auto.cs"; + var path = Path.GetFullPath(originPath); + if (File.Exists(path)) + buildScript = AssetDatabase.LoadAssetAtPath(originPath); + } + [Button(SdfIconType.Trash, Name = ""), HorizontalGroup(GroupID = "BS", Width = 50)] + void Delete() + { + AssetDatabase.DeleteAsset($"{savePath}/{className}.cs"); + buildScript = null; + } + [Sirenix.OdinInspector.PropertySpace(30)] + [Button(Icon = SdfIconType.Gem, Name = "生成"), HorizontalGroup(GroupID = "AA" /*, Width = 150*/,MinWidth =200,MarginLeft =0.3f,MarginRight =0.3f)] + public void Build() + { + var str = TEUIHelper.Generate(type == Type.listener || type == Type.all, type == Type.unitask || type == Type.all, root, nameSpace, className); + if (string.IsNullOrEmpty(str)) + { + Debug.LogError("出错啦,请检查是否选中root为空" + root == null); + return; + } + var originPath = $"{savePath}/{className}_Auto.cs"; + var path = Path.GetFullPath(originPath); + MakeSure(path); + File.WriteAllText(path, str); + AssetDatabase.Refresh(); + buildScript= AssetDatabase.LoadAssetAtPath(originPath); + Debug.Log($"保存完毕:Cs->{className}.cs ,位置:{path}"); + } + + public void OnInit(Transform root) + { + this.root = root; + this.className = this.root.name; + Load(); + } + [MenuItem("GameObject/ScriptGenerator/BuildWindow", priority = 40)] + static void OpenWindow() + { + var root = Selection.activeTransform; + if (root != null && (PrefabUtility.IsPartOfAnyPrefab(root) || (PrefabStageUtility.GetCurrentPrefabStage() != null && PrefabStageUtility.GetCurrentPrefabStage().IsPartOfPrefabContents(root.gameObject)))) + { + var window = GetWindow(); + // 设置窗口大小 + window.minSize = new Vector2(600, 250); + window.OnInit(root); + + } + else + Debug.LogError("仅支持预制体,请选中预制体"); + } + static void OpenTopWindow() + { + var window = EditorWindow.GetWindow(); + //Debug.Log("windiw" + window.name + "_aaa"); + window.position = new Rect(0, 0, 600, 800); + } + + public static void MakeSure(string path) + { + path = path.Replace("\\", "/"); + var extion = Path.GetExtension(path); + + var dire = System.IO.Path.GetDirectoryName(path); + if (System.IO.Directory.Exists(dire) == false) + System.IO.Directory.CreateDirectory(dire); + + if (string.IsNullOrEmpty(extion) == false) + { + if (System.IO.File.Exists(path) == false) + System.IO.File.Create(path).Dispose(); + } + } + } +} + diff --git a/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs.meta b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs.meta new file mode 100644 index 00000000..21a0001f --- /dev/null +++ b/UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f1012810e422c9249a0e302985d02b45 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: