diff --git a/UnityGame/Assets/Editor/GameUI/UIExportToolEditor.cs b/UnityGame/Assets/Editor/GameUI/UIExportToolEditor.cs index bb2bfc0..3a27b44 100644 --- a/UnityGame/Assets/Editor/GameUI/UIExportToolEditor.cs +++ b/UnityGame/Assets/Editor/GameUI/UIExportToolEditor.cs @@ -13,6 +13,10 @@ public class UIExportToolEditor : Editor private ReorderableList reorderableList; private void OnEnable() + { + } + + private void InitReorderableList() { reorderableList = new ReorderableList( serializedObject, @@ -82,8 +86,17 @@ public class UIExportToolEditor : Editor public override void OnInspectorGUI() { serializedObject.Update(); - reorderableList.DoLayoutList(); + if (reorderableList == null) + { + InitReorderableList(); + } + reorderableList?.DoLayoutList(); serializedObject.ApplyModifiedProperties(); + + if (GUILayout.Button("导出代码")) + { + ExportCode(); + } } /// @@ -99,4 +112,57 @@ public class UIExportToolEditor : Editor || component is Dropdown || component is InputField; } + /// + /// 导出代码 + /// + private void ExportCode() + { + string savePath = Application.dataPath + "/Scripts/GameLogic/Export/UGUI/"; + if (string.IsNullOrEmpty(savePath)) + return; + + string className = $"UI{target.GameObject().name}"; + string filename = System.IO.Path.Combine(savePath, $"{className}.cs"); + + var sb = new System.Text.StringBuilder(); + + sb.AppendLine("using UnityEngine;"); + sb.AppendLine("using UnityEngine.UI;"); + sb.AppendLine(); + sb.AppendLine("namespace CreatGame.UI"); + sb.AppendLine("{"); + sb.AppendLine($" public class {className} : UIViewBase"); + sb.AppendLine(" {"); + + // 字段定义 + + for (int i = 0; i < reorderableList.count; i++) + { + SerializedProperty element = reorderableList.serializedProperty.GetArrayElementAtIndex(i); + sb.AppendLine(" /// "); + sb.AppendLine(" /// "); + sb.AppendLine(" /// "); + + sb.AppendLine($" public {element.FindPropertyRelative("selectedComponentName").stringValue} {element.FindPropertyRelative("key").stringValue}"); + } + sb.AppendLine(" public override void PreLoad(GameObject view)"); + sb.AppendLine(" {"); + sb.AppendLine(" base.PreLoad(view);"); + sb.AppendLine(); + for (int i = 0; i < reorderableList.count; i++) + { + SerializedProperty element = reorderableList.serializedProperty.GetArrayElementAtIndex(i); + var filedName = element.FindPropertyRelative("key").stringValue; + var typeName = element.FindPropertyRelative("selectedComponentName").stringValue; + sb.AppendLine($" {filedName} = GetGameObject(nameof({filedName})).GetComponent<{typeName}>();"); + } + sb.AppendLine(" }"); + sb.AppendLine(" }"); + sb.AppendLine("}"); + Debug.Log(sb.ToString()); + System.IO.File.WriteAllText(filename, sb.ToString()); + Debug.Log($"✅ 导出成功:{filename}"); + + AssetDatabase.Refresh(); + } } \ No newline at end of file diff --git a/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs b/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs new file mode 100644 index 0000000..21c10cc --- /dev/null +++ b/UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using CreatGame.UI; +using UnityEngine; +using UnityEngine.UI; + +namespace CreatGame.UI +{ + public class UIMainView : UIViewBase + { + /// + /// + /// + public Button StarBtn; + /// + /// + /// + public Text StarBtnText; + public override void PreLoad(GameObject view) + { + base.PreLoad(view); + + StarBtn = GetGameObject(nameof(StarBtn)).GetComponent public virtual string PrefabPath { get; set; } /// + /// 窗口预制件 + /// + protected GameObject m_ViewObject; + /// + /// 导出脚本 + /// + protected UIExportTool m_ExportTool; + /// /// 加载窗口的时候需要预先加载的东西 /// - public virtual void PreLoad() + public virtual void PreLoad(GameObject viewObject) { + m_ViewObject = viewObject; + + m_ExportTool = viewObject.GetComponent(); } /// /// 初始化界面 @@ -26,5 +39,18 @@ { } + + protected GameObject GetGameObject(string name) + { + for (int i = 0; i < m_ExportTool.entries.Count; i++) + { + if (m_ExportTool.entries[i].key == name) + { + return m_ExportTool.entries[i].prefab; + } + } + + return null; + } } } \ No newline at end of file