From d12748e532adbda135f9f196a17808cc3d911a54 Mon Sep 17 00:00:00 2001
From: TongZiGang <754383023@qq.com>
Date: Wed, 16 Jul 2025 18:10:09 +0800
Subject: [PATCH] =?UTF-8?q?=20=E4=BB=A3=E7=A0=81=E5=AF=BC=E5=87=BA?=
=?UTF-8?q?=E5=B7=A5=E5=85=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Editor/GameUI/UIExportToolEditor.cs | 68 ++++++++++++++++++-
.../GameLogic/Export/UGUI/UIMainView.cs | 27 ++++++++
.../GameLogic/Export/UGUI/UIMainView.cs.meta | 11 +++
.../Assets/Scripts/GameLogic/UI/UIManager.cs | 1 -
.../Assets/Scripts/GameLogic/UI/UIViewBase.cs | 30 +++++++-
5 files changed, 133 insertions(+), 4 deletions(-)
create mode 100644 UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs
create mode 100644 UnityGame/Assets/Scripts/GameLogic/Export/UGUI/UIMainView.cs.meta
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