代码导出工具

This commit is contained in:
2025-07-16 18:10:09 +08:00
parent 60c1185b87
commit d12748e532
5 changed files with 133 additions and 4 deletions

View File

@@ -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();
}
}
/// <summary>
@@ -99,4 +112,57 @@ public class UIExportToolEditor : Editor
|| component is Dropdown
|| component is InputField;
}
/// <summary>
/// 导出代码
/// </summary>
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(" /// <summary>");
sb.AppendLine(" /// ");
sb.AppendLine(" /// </summary>");
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();
}
}

View File

@@ -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
{
/// <summary>
///
/// </summary>
public Button StarBtn;
/// <summary>
///
/// </summary>
public Text StarBtnText;
public override void PreLoad(GameObject view)
{
base.PreLoad(view);
StarBtn = GetGameObject(nameof(StarBtn)).GetComponent<Button>();
StarBtnText = GetGameObject(nameof(StarBtnText)).GetComponent<Text>();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c5ae2248a663cd4f9fc66ca55751f19
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -26,7 +26,6 @@ namespace CreatGame.UI
{
var view = new T();
view.PreLoad();
view.InitView();
return view;
}

View File

@@ -1,4 +1,6 @@
namespace CreatGame.UI
using UnityEngine;
namespace CreatGame.UI
{
public class UIViewBase
{
@@ -7,10 +9,21 @@
/// </summary>
public virtual string PrefabPath { get; set; }
/// <summary>
/// 窗口预制件
/// </summary>
protected GameObject m_ViewObject;
/// <summary>
/// 导出脚本
/// </summary>
protected UIExportTool m_ExportTool;
/// <summary>
/// 加载窗口的时候需要预先加载的东西
/// </summary>
public virtual void PreLoad()
public virtual void PreLoad(GameObject viewObject)
{
m_ViewObject = viewObject;
m_ExportTool = viewObject.GetComponent<UIExportTool>();
}
/// <summary>
/// 初始化界面
@@ -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;
}
}
}