【发布时间】:2022-08-24 13:45:25
【问题描述】:
在 Unity3D 脚本中,有没有办法在给定与使用 CustomPropertyDrawer 关联的类型的情况下获取 PropertyDrawer 派生类?所以在下面的例子中:
using UnityEngine;
using UnityEditor;
public struct MyCustomType {
...
}
[CustomPropertyDrawer(typeof(MyCustomType))]
public class MyCustomTypeDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
...
}
}
...如果给定MyCustomType,此方法将返回MyCystomTypeDrawer(最好是Type)。但它是通用的,适用于使用CustomPropertyDrawer 与之关联的PropertyDrawer 派生类的任何类型。
-
你绝对可以使用反射。浏览所有加载的程序集,查看每个声明的类型,并检查该类型是否扩展了 PropertyDrawer。然后,您需要获取传递给 CustomPropertyDrawerAttribute 的类型(内部类型 m_Type),然后检查该类型是否与传递给您的函数的类型相同。