using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.UIElements; using UnityEditor.UIElements; using MuHua; namespace MuHuaEditor { /// /// 编辑器 /// public abstract class ModuleUIEditor : Editor where T : Object { /// UXML 资源 public VisualTreeAsset VisualTreeAsset = default; /// 选中目标 protected T value; /// 绑定文档 public VisualElement Element = null; /// 控件列表 public List controls = new List(); public virtual void Awake() => value = target as T; /// 在OnEnable中注册 private void OnEnable() => EditorApplication.update += Update; /// 在OnDisable中注销 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()); /// 初始化 public abstract void Initial(); /// 添加控件 public void AddControl(UIControl control) => controls.Add(control); /// 移除控件 public void RemoveControl(UIControl control) => controls.Remove(control); /// 查询UI元素 public VE Q(string name = null, string className = null) where VE : VisualElement => Element.Q(name, className); } }