我第一次尝试使用 Unity3D 的PropertyDrawer。
我复制并粘贴了在博客中找到的示例,但它似乎不起作用。
这是一个仍然不起作用的简化版本:
//a serializable class, it should be possible write an custom property drawer for it
[System.Serializable]
public class SimpleClass
{
public int myField;
}
这是一个没有任何东西的空抽屉:
[CustomPropertyDrawer (typeof (SimpleClass))]
public class SimpleClassDrawer : PropertyDrawer{
public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label)
{
SerializedProperty myField= prop.FindPropertyRelative ("myField");
//here's the problem: myField always null
}
最后是具有以下公共字段的 MonoBehavior SimpleClass
:
public class Test : MonoBehaviour {
public SimpleClass s;
}
的OnGUI
方法SimpleClassDrawer
总是被调用,但myField
总是为空。
我不明白我错过了什么?有什么线索吗?
编辑:
经过更深入的调查,它似乎OnGUI
被调用了两次。第二次该属性为 null 并NullReferenceExceptionw
在我尝试通过绘制它时
抛出一个EditorGUI.PropertyField
。
编辑2:
我的帖子的最后一个版本包含一些复制和粘贴错误。我什至尝试使用 Jerdak 发布的代码,但问题至少在 Unity 4.2 中仍然存在。这是堆栈跟踪:
NullReferenceException: Curve: SerializedProperty is null UnityEditor.EditorGUI.BeginProperty (Rect totalPosition, UnityEngine.GUIContent label, UnityEditor.SerializedProperty property) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3278) UnityEditor .EditorGUI.SinglePropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3760) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签,布尔值 includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,统一引擎。GUIContent 标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (在 Assets/Scripts/Editor /Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs :3746) UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签,布尔值 includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor。 EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine。GUIContent 标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (在 Assets/Scripts/Editor /Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs :3746) UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签,布尔值 includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor。 EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,Boolean includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3683)UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性)(在 C:/BuildAgent/work/cac08d8a5e25d4cb /Editor/MonoGenerated/Editor/EditorGUI.cs:3679) UnityEditor.Editor.OptimizedInspectorGUIImplementation (Rect contentRect) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189) UnityEditor.GenericInspector.OnOptimizedInspectorGUI (Rect contentRect) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/GenericInspector.cs:46) UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (在 C: /BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:第864章 : Reflection.Binder binder,System.Object[] 参数,System.Globalization.CultureInfo 文化)(位于 /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)