界面组件导出工具
This commit is contained in:
145
UnityGame/Assets/Editor/GameUI/UIComponentExportEditor.cs
Normal file
145
UnityGame/Assets/Editor/GameUI/UIComponentExportEditor.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using CreatGame.UI;
|
||||
using UnityEditorInternal;
|
||||
using Unity.VisualScripting;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[CustomEditor(typeof(UIComponentExport))]
|
||||
public class UIComponentExportEditor : Editor
|
||||
{
|
||||
private ReorderableList reorderableList;
|
||||
|
||||
private void InitReorderableList()
|
||||
{
|
||||
reorderableList = new ReorderableList(
|
||||
serializedObject,
|
||||
serializedObject.FindProperty("entries"),
|
||||
true, true, true, true);
|
||||
|
||||
reorderableList.drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, "UI Prefab Entries"); };
|
||||
|
||||
reorderableList.elementHeightCallback = (int index) => { return EditorGUIUtility.singleLineHeight + 6f; };
|
||||
|
||||
reorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
|
||||
{
|
||||
SerializedProperty element = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
|
||||
// drawElementCallback 中的内容
|
||||
SerializedProperty keyProp = element.FindPropertyRelative("key");
|
||||
SerializedProperty prefabProp = element.FindPropertyRelative("prefab");
|
||||
SerializedProperty selectedNameProp = element.FindPropertyRelative("selectedComponentName");
|
||||
|
||||
GameObject prefab = prefabProp.objectReferenceValue as GameObject;
|
||||
|
||||
// 查找组件类型
|
||||
List<string> uguiComponentNames = new List<string>();
|
||||
if (prefab != null)
|
||||
{
|
||||
var components = prefab.GetComponents<Component>();
|
||||
uguiComponentNames = prefab.GetComponents<Component>()
|
||||
.Where(c => c != null && UIViewExportEditor.IsUGUIComponent(c))
|
||||
.Select(c => c.GetType().Name)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
float lineHeight = EditorGUIUtility.singleLineHeight;
|
||||
float padding = 4f;
|
||||
float thirdWidth = (rect.width - 2 * padding) / 3f;
|
||||
|
||||
Rect keyRect = new Rect(rect.x, rect.y + 2, thirdWidth, lineHeight);
|
||||
Rect prefabRect = new Rect(rect.x + thirdWidth + padding, rect.y + 2, thirdWidth, lineHeight);
|
||||
Rect popupRect = new Rect(rect.x + 2 * (thirdWidth + padding), rect.y + 2, thirdWidth, lineHeight);
|
||||
|
||||
// key 字段
|
||||
EditorGUI.PropertyField(keyRect, keyProp, GUIContent.none);
|
||||
|
||||
// prefab 字段
|
||||
EditorGUI.PropertyField(prefabRect, prefabProp, GUIContent.none);
|
||||
|
||||
// 下拉框
|
||||
if (uguiComponentNames.Count > 0)
|
||||
{
|
||||
int selectedIndex = Mathf.Max(0, uguiComponentNames.IndexOf(selectedNameProp.stringValue));
|
||||
selectedIndex = EditorGUI.Popup(popupRect, selectedIndex, uguiComponentNames.ToArray());
|
||||
selectedNameProp.stringValue = uguiComponentNames[selectedIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUI.LabelField(popupRect, "无UGUI组件");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
if (reorderableList == null)
|
||||
{
|
||||
InitReorderableList();
|
||||
}
|
||||
reorderableList?.DoLayoutList();
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
if (GUILayout.Button("导出代码"))
|
||||
{
|
||||
ExportCode();
|
||||
}
|
||||
}
|
||||
|
||||
/// <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 partial class {className} : UIComponentBase");
|
||||
sb.AppendLine(" {");
|
||||
sb.AppendLine($" public override string PrefabPath => \"Prefabs/UI/{target.GameObject().name}\";");
|
||||
|
||||
// 字段定义
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75e425207f8b4c369bf1a58bd14d8cb4
|
||||
timeCreated: 1752945208
|
||||
@@ -8,8 +8,8 @@ using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
[CustomEditor(typeof(UIExportTool))]
|
||||
public class UIExportToolEditor : Editor
|
||||
[CustomEditor(typeof(UIViewExport))]
|
||||
public class UIViewExportEditor : Editor
|
||||
{
|
||||
private ReorderableList reorderableList;
|
||||
|
||||
@@ -104,7 +104,7 @@ public class UIExportToolEditor : Editor
|
||||
/// <summary>
|
||||
/// 判断是否是常见UGUI组件(你可以自行扩展)
|
||||
/// </summary>
|
||||
private bool IsUGUIComponent(Component component)
|
||||
public static bool IsUGUIComponent(Component component)
|
||||
{
|
||||
return component is Graphic // 基类,包含 Image、Text、RawImage
|
||||
|| component is Button
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace CreatGame.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用的导出预制件的基类
|
||||
/// </summary>
|
||||
public class UIComponentBase : MonoBehaviour
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba52eedd767b46ae90e720030c75503f
|
||||
timeCreated: 1752945611
|
||||
@@ -16,7 +16,7 @@ namespace CreatGame.UI
|
||||
/// <summary>
|
||||
/// 导出脚本
|
||||
/// </summary>
|
||||
protected UIExportTool m_ExportTool;
|
||||
protected UIViewExport MViewExport;
|
||||
/// <summary>
|
||||
/// 是否加载完成
|
||||
/// </summary>
|
||||
@@ -27,7 +27,7 @@ namespace CreatGame.UI
|
||||
public virtual void PreLoad(GameObject viewObject)
|
||||
{
|
||||
m_ViewObject = viewObject;
|
||||
m_ExportTool = viewObject.GetComponent<UIExportTool>();
|
||||
MViewExport = viewObject.GetComponent<UIViewExport>();
|
||||
IsPreLoad = true;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -47,11 +47,11 @@ namespace CreatGame.UI
|
||||
|
||||
protected GameObject GetGameObject(string name)
|
||||
{
|
||||
for (int i = 0; i < m_ExportTool.entries.Count; i++)
|
||||
for (int i = 0; i < MViewExport.entries.Count; i++)
|
||||
{
|
||||
if (m_ExportTool.entries[i].key == name)
|
||||
if (MViewExport.entries[i].key == name)
|
||||
{
|
||||
return m_ExportTool.entries[i].prefab;
|
||||
return MViewExport.entries[i].prefab;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
19
UnityGame/Assets/Scripts/GameTools/UI/UIComponentExport.cs
Normal file
19
UnityGame/Assets/Scripts/GameTools/UI/UIComponentExport.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CreatGame.UI
|
||||
{
|
||||
public class UIComponentExport : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public class UIEntry
|
||||
{
|
||||
public string key;
|
||||
public GameObject prefab;
|
||||
public string selectedComponentName; // 存储选择的组件类型名
|
||||
}
|
||||
|
||||
public List<UIEntry> entries = new List<UIEntry>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d94101161a144f17b4b80530abe93975
|
||||
timeCreated: 1752945103
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class UIExportTool : MonoBehaviour
|
||||
public class UIViewExport : MonoBehaviour
|
||||
{
|
||||
[Serializable]
|
||||
public class UIEntry
|
||||
Reference in New Issue
Block a user