This commit is contained in:
CortexCore
2024-03-31 23:31:00 +08:00
parent e179d2eb53
commit b7b89ee71a
641 changed files with 31286 additions and 22134 deletions

View File

@@ -5,6 +5,7 @@ using UnityEngine.UIElements;
using System.Reflection;
using System;
#if UNITY_EDITOR
using MackySoft.SerializeReferenceExtensions.Editor;
using UnityEditor;
using Editor = UnityEditor.Editor;
using UnityEditor.UIElements;
@@ -16,6 +17,7 @@ namespace BITKit
}
#if UNITY_EDITOR
[CanEditMultipleObjects]
[CustomEditor(typeof(MonoBehaviour),true)]
public class MonoBehaviorInspector : BITInspector<MonoBehaviour>
{
@@ -46,6 +48,89 @@ namespace BITKit
}
return (T)field.GetValue(obj);
}
public static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, bool hideScript)
{
container.Clear();
if (serializedObject.targetObject is null)
{
var label = container.Create<Label>();
label.text = "Null";
return;
}
var property = serializedObject.GetIterator();
if (!property.NextVisible(true)) return; // Expand first child.
do
{
if (property.propertyPath == "m_Script" && hideScript)
{
continue;
}
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)))
{
var attribute = fieldInfo.GetCustomAttribute<ReadOnlyAttribute>();
var _container = container.Create<VisualElement>();
_container.style.flexDirection = FlexDirection.Row;
if (attribute.HideLabel is false)
{
_container.Create<Label>().text = $"{property.displayName}:";
_container.Create<VisualElement>().style.flexGrow = 1;
}
_container.Create<Label>().bindingPath = property.propertyPath;
}
else if (PropertyDrawerCache.TryGetPropertyDrawer(type, out var drawer))
{
var ve = drawer.CreatePropertyGUI(property);
container.Add(ve);
}
else
{
//var label = container.Create<Label>();
//label.text =$"propertyPath:{property.propertyPath} fieldInfo:{fieldInfo} type:{type} fieldInfo:{fieldInfo}";
var field = new PropertyField(property)
{
name = "PropertyField:" + property.propertyPath
};
if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
{
field.SetEnabled(false);
}
container.Add(field);
}
// 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);
// }
} while (property.NextVisible(false));
foreach (var method in serializedObject.targetObject.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (method.GetCustomAttribute<BITAttribute>() is null) continue;
if (method.GetParameters().Length is not 0) continue;
var button = new Button(() => method.Invoke(serializedObject.targetObject, null))
{
text = method.Name
};
container.Add(button);
}
}
}
public class BITInspector<T> : Editor
{
@@ -113,96 +198,26 @@ namespace BITKit
root.root.styleSheets.Add(css);
EditorApplication.update += OnUpdate;
OnEnabled();
}
protected virtual void OnEnabled(){}
void OnDisable()
{
EditorApplication.update -= OnUpdate;
OnDisabled();
}
protected virtual void OnDisabled(){}
protected virtual void OnUpdate()
{
}
protected void FillDefaultInspector()
{
root.root.Clear();
FillDefaultInspector(root, serializedObject, true);
}
protected static void FillDefaultInspector(VisualElement container, SerializedObject serializedObject, bool hideScript)
{
container.Clear();
if (serializedObject.targetObject is null)
{
var label = container.Create<Label>();
label.text = "Null";
return;
}
var property = serializedObject.GetIterator();
if (!property.NextVisible(true)) return; // Expand first child.
do
{
if (property.propertyPath == "m_Script" && hideScript)
{
continue;
}
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)))
{
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;
}
else
{
//var label = container.Create<Label>();
//label.text =$"propertyPath:{property.propertyPath} fieldInfo:{fieldInfo} type:{type} fieldInfo:{fieldInfo}";
var field = new PropertyField(property)
{
name = "PropertyField:" + property.propertyPath
};
if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
{
field.SetEnabled(false);
}
container.Add(field);
}
// 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);
// }
} while (property.NextVisible(false));
foreach (var method in serializedObject.targetObject.GetType().GetMethods())
{
if (method.GetCustomAttribute<BITAttribute>() is null) continue;
if (method.GetParameters().Length is not 0) continue;
var button = new Button(() => method.Invoke(serializedObject.targetObject, null))
{
text = method.Name
};
container.Add(button);
}
BITInspectorExtensions.FillDefaultInspector(root, serializedObject, true);
}
}
public class BITProperty : PropertyDrawer