新增UI编辑器功能

This commit is contained in:
MuHua-123
2025-10-04 09:28:18 +08:00
parent f4a07224aa
commit 9be153994f
8 changed files with 124 additions and 34 deletions
+65
View File
@@ -0,0 +1,65 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using MuHua;
namespace MuHuaEditor {
/// <summary>
/// 编辑器
/// </summary>
public abstract class ModuleUIEditor<T> : Editor where T : Object {
/// <summary> UXML 资源 </summary>
public VisualTreeAsset VisualTreeAsset = default;
/// <summary> 选中目标 </summary>
protected T value;
/// <summary> 绑定文档 </summary>
public VisualElement Element = null;
/// <summary> 控件列表 </summary>
public List<UIControl> controls = new List<UIControl>();
public virtual void Awake() => value = target as T;
/// <summary> 在OnEnable中注册 </summary>
private void OnEnable() => EditorApplication.update += Update;
/// <summary> 在OnDisable中注销 </summary>
private void OnDisable() => EditorApplication.update -= Update;
public override VisualElement CreateInspectorGUI() {
VisualElement root = new VisualElement();
// 1. 添加 Script 字段(只读)
var scriptProperty = serializedObject.FindProperty("m_Script");
if (scriptProperty != null) {
var scriptField = new PropertyField(scriptProperty);
scriptField.SetEnabled(false); // 只读
root.Add(scriptField);
}
// 实例化 UXML
if (VisualTreeAsset != null) {
Element = VisualTreeAsset.Instantiate();
root.Add(Element);
Initial();
}
// 如果没设置 UXML,返回默认 Inspector
return root ?? new IMGUIContainer(() => DrawDefaultInspector());
}
public virtual void Update() => controls.ForEach(control => control.Update());
public virtual void OnDestroy() => controls.ForEach(control => control.Dispose());
/// <summary> 初始化 </summary>
public abstract void Initial();
/// <summary> 添加控件 </summary>
public void AddControl(UIControl control) => controls.Add(control);
/// <summary> 移除控件 </summary>
public void RemoveControl(UIControl control) => controls.Remove(control);
/// <summary> 查询UI元素 </summary>
public VE Q<VE>(string name = null, string className = null) where VE : VisualElement => Element.Q<VE>(name, className);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a8a211ecbf2acb642a002e2ccac32de4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using MuHua;
namespace MuHuaEditor {
/// <summary>
/// 编辑器窗口
/// </summary>
public abstract class ModuleUIEditorWindow : EditorWindow {
/// <summary> UXML 资源 </summary>
public VisualTreeAsset VisualTreeAsset = default;
/// <summary> 绑定文档 </summary>
public VisualElement Element;
/// <summary> 控件列表 </summary>
public List<UIControl> controls = new List<UIControl>();
public virtual void CreateGUI() {
// Instantiate UXML
Element = VisualTreeAsset.Instantiate();
Element.style.flexGrow = 1;
rootVisualElement.Add(Element);
Initial();
}
public virtual void Update() => controls.ForEach(control => control.Update());
public virtual void OnDestroy() => controls.ForEach(control => control.Dispose());
/// <summary> 初始化 </summary>
public abstract void Initial();
/// <summary> 添加控件 </summary>
public void AddControl(UIControl control) => controls.Add(control);
/// <summary> 移除控件 </summary>
public void RemoveControl(UIControl control) => controls.Remove(control);
/// <summary> 查询UI元素 </summary>
public T Q<T>(string name = null, string className = null) where T : VisualElement => Element.Q<T>(name, className);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7a2fd8030b7392643995372e5c88631f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,18 @@
{
"name": "MuHuaEditor.UITool",
"rootNamespace": "",
"references": [
"GUID:6206345d516a2bb4a821a7ee90e1b5d5"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 3a65c91317a32ac438ed8542cf646c2f
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: