2023-06-08 14:09:50 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
using System.Reflection;
|
|
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
#if UNITY_EDITOR
|
2023-08-23 01:59:40 +08:00
|
|
|
using System;
|
2023-06-08 14:09:50 +08:00
|
|
|
using UnityEditor;
|
|
|
|
using Editor = UnityEditor.Editor;
|
|
|
|
using UnityEditor.UIElements;
|
|
|
|
#else
|
|
|
|
using Editor=BITKit.Constant.EmetyClass;
|
|
|
|
#endif
|
|
|
|
namespace BITKit
|
|
|
|
{
|
|
|
|
public class BITAttribute : System.Attribute
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
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
|
|
|
public class ServerRpcAttribute : System.Attribute
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
public class ClientRpcAttribute : System.Attribute
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#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();
|
|
|
|
var field = type.GetField(self.propertyPath);
|
|
|
|
return (T)field.GetValue(obj);
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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-08-12 01:43:24 +08:00
|
|
|
var property = serializedObject.GetIterator();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
var type = serializedObject.targetObject.GetType().GetField(property.name);
|
2023-08-23 01:59:40 +08:00
|
|
|
if ( type is not null && Attribute.IsDefined(type.FieldType, typeof(ReadOnlyAttribute)))
|
2023-08-12 01:43:24 +08:00
|
|
|
{
|
2023-08-23 01:59:40 +08:00
|
|
|
container.Create<Label>().bindingPath = property.propertyPath;
|
|
|
|
}
|
|
|
|
else
|
2023-08-12 01:43:24 +08:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
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-08-23 01:59:40 +08:00
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
}
|
|
|
|
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))
|
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
|
|
|
|
}
|