我希望用PropertyAttribute标记来使 一个类里引用的一个组件 或者一个ScriptableObject能在这个组件的inspector中显示 可以方便修改 不知要怎么实现 下面是尝试的一个方案 但是结果并不理想
效果:未引用对象:
将引用的对象:
引用对象后:
public class ShowObject: PropertyAttribute { }
namespace UnityEditor
{
[CustomPropertyDrawer(typeof(ShowObject))]
public class ShowObjectDraw : PropertyDrawer
{
//画嵌入的Inspector的Editor
Editor objectEditor;
//要画的对象
UnityEngine.Object target;
//展开
bool open;
//PropertyDrawer的高度
float height;
//动态锚点
Rect ancot;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return height + 5;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//初始化锚点
ancot =new Rect(position);
//初始化高度
height = 17;
ancot.height = 17;
//如果目标对象存在则画展开框
if (target != null)
{
open = EditorGUI.InspectorTitlebar(ancot, open, target, true);
ancot.yMin += 20;
ancot.height += 17;
height += 20;
}
//属性基本赋值框绘制
EditorGUI.ObjectField(ancot, property, label);
//目标获取
target = property.objectReferenceValue;
//是否展开
if (open )
{
objectEditor = Editor.CreateEditor(target);
//绘制对象的Inspector——不知道为什么它绘制在 载体组件的后面 导致会挡住 载体组件后面的组件显示
objectEditor.OnInspectorGUI();
}
else {
objectEditor = null;
}
}
}
}
组件对象:
public class Team_TypeLogicFSM
{
[ShowObject]
public Type_AIData con;
}
有没有知道怎么实现的大佬给个点子 或者EditorGUI中是否有我没发现的方法可以使用的?