BITKit/Packages/Runtime/Utility/Editor/BITInspector.cs

172 lines
5.1 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System.Reflection;
using UnityEngine.AddressableAssets;
#if UNITY_EDITOR
using UnityEditor;
using Editor = UnityEditor.Editor;
using UnityEditor.UIElements;
#else
using Editor=BITKit.Constant.EmetyClass;
#endif
namespace BITKit
{
public class BITAttribute : System.Attribute
{
}
public class ServerRpcAttribute : System.Attribute
{
}
public class ClientRpcAttribute : System.Attribute
{
}
#if UNITY_EDITOR
public class BITInspector<T> : Editor
{
const string ussName = "BITInspector";
public class VisualElementCreator
{
public static implicit operator VisualElement(VisualElementCreator self)
{
return self.root;
}
public VisualElement root;
public VE Create<VE>() where VE : VisualElement, new()
{
root = root ?? new();
var ve = new VE();
root.Add(ve);
return ve;
}
}
protected VisualElementCreator root = new();
protected T agent;
public override VisualElement CreateInspectorGUI()
{
if (serializedObject.targetObject is ICustomInspector inspector && inspector.GetVisualElement() is not null)
{
return inspector.GetVisualElement();
}
else
{
FillDefaultInspector();
return root;
}
}
protected Label CreateSubTitle(string value)
{
var label = root.Create<Label>();
label.text = value;
label.AddToClassList("subTitle");
return label;
}
void Awake()
{
root.root = new();
if (serializedObject.targetObject is T value)
{
agent = value;
}
}
void OnEnable()
{
//StyleSheet css = Addressables.LoadAssetAsync<StyleSheet>(ussName).WaitForCompletion();
StyleSheet css = AssetDatabase.LoadAssetAtPath<StyleSheet>($"Assets/BITKit/UX/BITInspector.uss");
root.root.styleSheets.Add(css);
EditorApplication.update += OnUpdate;
}
void OnDisable()
{
EditorApplication.update -= OnUpdate;
}
protected virtual void OnUpdate()
{
}
protected void FillDefaultInspector()
{
FillDefaultInspector(root, serializedObject, true);
}
public static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, bool hideScript)
{
SerializedProperty property = serializedObject.GetIterator();
if (property.NextVisible(true)) // Expand first child.
{
do
{
if (property.propertyPath == "m_Script" && hideScript)
{
continue;
}
var type = serializedObject.targetObject.GetType().GetField(property.name);
var field = new PropertyField(property);
field.name = "PropertyField:" + property.propertyPath;
if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
{
field.SetEnabled(false);
}
try
{
if (type is not null)
{
var header = type.GetCustomAttribute<HeaderAttribute>();
if (header is not null)
{
Label label = new Label(header.header);
label.AddToClassList("subTitle");
container.Add(label);
}
}
}
catch (System.Exception e)
{
Debug.LogException(e);
}
container.Add(field);
}
while (property.NextVisible(false));
foreach (var method in serializedObject.targetObject.GetType().GetMethods())
{
if (method.GetCustomAttribute<BITAttribute>() is not null)
{
if (method.GetParameters().Length is 0)
{
var button = new Button(() => method.Invoke(serializedObject.targetObject, null));
button.text = method.Name;
container.Add(button);
}
}
}
}
}
}
public class BITProperty : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
PropertyField propertyField = new(property);
return propertyField;
}
}
#endif
}