更改文件架构
This commit is contained in:
175
Packages/Common~/Scripts/Utility/Editor/BITInspector.cs
Normal file
175
Packages/Common~/Scripts/Utility/Editor/BITInspector.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
protected virtual void OnAwake(){}
|
||||
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;
|
||||
}
|
||||
protected virtual void OnEnabled(){}
|
||||
void OnDisable()
|
||||
{
|
||||
EditorApplication.update -= OnUpdate;
|
||||
}
|
||||
protected virtual void OnDisabled(){}
|
||||
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
|
||||
}
|
11
Packages/Common~/Scripts/Utility/Editor/BITInspector.cs.meta
Normal file
11
Packages/Common~/Scripts/Utility/Editor/BITInspector.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 24c0970857cded548bda75191c8317f0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user