BITFALL/Assets/BITKit/Unity/Scripts/Utility/Editor/BITInspector.cs

217 lines
7.4 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System.Reflection;
2023-08-23 01:59:40 +08:00
using System;
2023-10-02 23:24:56 +08:00
#if UNITY_EDITOR
2023-06-08 14:09:50 +08:00
using UnityEditor;
using Editor = UnityEditor.Editor;
using UnityEditor.UIElements;
#endif
namespace BITKit
{
public class BITAttribute : System.Attribute
{
}
2023-10-20 19:31:12 +08:00
#if UNITY_EDITOR
2023-10-02 23:24:56 +08:00
[CustomEditor(typeof(MonoBehaviour),true)]
public class MonoBehaviorInspector : BITInspector<MonoBehaviour>
2023-08-27 02:58:19 +08:00
{
2023-10-02 23:24:56 +08:00
2023-08-27 02:58:19 +08:00
}
2023-10-20 19:31:12 +08:00
#endif
2023-08-12 01:43:24 +08:00
public class BITEditorUtils
{
2023-08-23 01:59:40 +08:00
public const string InspectorPath = "Assets/BITKit/Unity/UX/BITInspector.uss";
public const string StylePath = "Assets/BITKit/Unity/UX/Common/Common.uss";
2023-08-12 01:43:24 +08:00
#if UNITY_EDITOR
2023-08-23 01:59:40 +08:00
public static StyleSheet Style => AssetDatabase.LoadAssetAtPath<StyleSheet>(StylePath);
2023-08-12 01:43:24 +08:00
public static StyleSheet InspectorStyleSheet => AssetDatabase.LoadAssetAtPath<StyleSheet>(InspectorPath);
#endif
}
2023-06-08 14:09:50 +08:00
#if UNITY_EDITOR
2023-08-23 01:59:40 +08:00
public static class BITInspectorExtensions
{
public static T Get<T>(this SerializedProperty self)
{
var obj = self.serializedObject.targetObject;
var type = obj.GetType();
2023-10-20 19:31:12 +08:00
var field = type.GetField(self.propertyPath, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (field is null)
{
throw new NullReferenceException($"Field {self.propertyPath} is null");
}
2023-08-23 01:59:40 +08:00
return (T)field.GetValue(obj);
}
}
2023-06-08 14:09:50 +08:00
public class BITInspector<T> : Editor
{
2023-10-20 19:31:12 +08:00
private const string ussName = "BITInspector";
2023-06-08 14:09:50 +08:00
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
{
2023-10-20 19:31:12 +08:00
try
{
FillDefaultInspector();
}
catch (Exception e)
{
Debug.LogException(e);
return base.CreateInspectorGUI();
}
2023-06-08 14:09:50 +08:00
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;
}
}
protected virtual void OnAwake(){}
void OnEnable()
{
//StyleSheet css = Addressables.LoadAssetAsync<StyleSheet>(ussName).WaitForCompletion();
2023-08-12 01:43:24 +08:00
StyleSheet css = AssetDatabase.LoadAssetAtPath<StyleSheet>(BITEditorUtils.InspectorPath);
2023-06-08 14:09:50 +08:00
root.root.styleSheets.Add(css);
EditorApplication.update += OnUpdate;
}
protected virtual void OnEnabled(){}
void OnDisable()
{
EditorApplication.update -= OnUpdate;
}
protected virtual void OnDisabled(){}
protected virtual void OnUpdate()
{
}
protected void FillDefaultInspector()
{
2023-10-20 19:31:12 +08:00
root.root.Clear();
2023-06-08 14:09:50 +08:00
FillDefaultInspector(root, serializedObject, true);
}
2023-08-12 01:43:24 +08:00
protected static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, bool hideScript)
2023-06-08 14:09:50 +08:00
{
2023-10-20 19:31:12 +08:00
container.Clear();
2023-10-29 15:27:13 +08:00
if (serializedObject.targetObject is null)
{
var label = container.Create<Label>();
label.text = "Null";
return;
}
2023-11-30 00:23:23 +08:00
var property = serializedObject.GetIterator();
2023-08-12 01:43:24 +08:00
if (!property.NextVisible(true)) return; // Expand first child.
do
2023-06-08 14:09:50 +08:00
{
2023-08-12 01:43:24 +08:00
if (property.propertyPath == "m_Script" && hideScript)
2023-06-08 14:09:50 +08:00
{
2023-08-12 01:43:24 +08:00
continue;
}
2023-10-20 19:31:12 +08:00
2023-11-30 00:23:23 +08:00
var type = serializedObject.targetObject.GetType();
var fieldInfo = serializedObject.targetObject.GetType().GetField(property.propertyPath, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo is not null && Attribute.IsDefined(fieldInfo, typeof(ReadOnlyAttribute)))
2023-08-12 01:43:24 +08:00
{
2023-11-30 00:23:23 +08:00
var _container = container.Create<VisualElement>();
_container.style.flexDirection = FlexDirection.Row;
_container.Create<Label>().text = $"{property.displayName}:";
_container.Create<VisualElement>().style.flexGrow = 1;
_container.Create<Label>().bindingPath = property.propertyPath;
2023-08-23 01:59:40 +08:00
}
else
2023-08-12 01:43:24 +08:00
{
2023-11-30 00:23:23 +08:00
//var label = container.Create<Label>();
//label.text =$"propertyPath:{property.propertyPath} fieldInfo:{fieldInfo} type:{type} fieldInfo:{fieldInfo}";
2023-08-23 01:59:40 +08:00
var field = new PropertyField(property)
{
name = "PropertyField:" + property.propertyPath
};
if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
{
field.SetEnabled(false);
}
2023-10-20 19:31:12 +08:00
2023-08-23 01:59:40 +08:00
container.Add(field);
2023-06-08 14:09:50 +08:00
}
2023-08-12 01:43:24 +08:00
// try
// {
// var header = type?.GetCustomAttribute<HeaderAttribute>();
// if (header != null)
// {
// var label = new Label(header.header);
// label.AddToClassList("subTitle");
// container.Add(label);
// }
// }
// catch (System.Exception e)
// {
// Debug.LogException(e);
// }
2023-10-20 19:31:12 +08:00
} while (property.NextVisible(false));
2023-08-12 01:43:24 +08:00
2023-12-03 17:35:43 +08:00
foreach (var method in serializedObject.targetObject.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
2023-08-12 01:43:24 +08:00
{
if (method.GetCustomAttribute<BITAttribute>() is null) continue;
if (method.GetParameters().Length is not 0) continue;
var button = new Button(() => method.Invoke(serializedObject.targetObject, null))
2023-06-08 14:09:50 +08:00
{
2023-08-12 01:43:24 +08:00
text = method.Name
};
2023-06-08 14:09:50 +08:00
2023-08-12 01:43:24 +08:00
container.Add(button);
2023-06-08 14:09:50 +08:00
}
}
}
public class BITProperty : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
PropertyField propertyField = new(property);
return propertyField;
}
}
#endif
}