我想第一次使用Unity3D的PropertyDrawer。SerializedProperty始終爲null Unity3D PropertyDrawers
我複製並粘貼了blog中的示例,但它似乎不起作用。
下面是仍然不能正常工作的簡化版本:
//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
方法總是調用,但myField
始終爲空。
我不明白我失蹤了什麼?任何線索?
編輯:
更深的調查之後,它似乎OnGUI
被稱爲兩次。第二次,該屬性爲空,並且每當我嘗試通過 EditorGUI.PropertyField
時拋出一個NullReferenceExceptionw
。
EDIT2:
帖子的最後一個版本containted幾個複製和粘貼錯誤。我甚至嘗試使用Jerdak發佈的代碼,但問題至少在Unity 4.2中保持不變。這是堆棧跟蹤:
的NullReferenceException:曲線:SerializedProperty爲空 UnityEditor.EditorGUI.BeginProperty(矩形totalPosition, UnityEngine.GUIContent標籤,UnityEditor.SerializedProperty屬性) (在 C:/ BuildAgent /工作/ cac08d8a5e25d4cb /編輯/ MonoGenerated /編輯/ EditorGUI.cs:3278) UnityEditor.EditorGUI.SinglePropertyField(矩形位置, UnityEditor.SerializedProperty屬性,UnityEngine.GUIContent標籤) (在 C:/ BuildAgent /工作/ cac08d8a5e25d4cb /編輯/ MonoGenerated/Editor/EditorGUI.cs:3760) UnityEditor.EditorGU I.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(矩形POS, UnityEditor.SerializedProperty prop,UnityEngine.GUIContent label)(在 Assets/Scripts/Editor/Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField(Rect position, 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(矩形POS,UnityEditor。 UnityEditor.EditorGUI.SinglePropertyField(Rect position,012)(編號 prop,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屬性,布爾includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3683) UnityEditor.EditorGUI.PropertyField(Rect position, UnityEditor.SerializedProperty屬性)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3679) UnityEditor.Editor.OptimizedInspectorGUIImplementation(矩形 contentRect)(在 C:/ BuildAgent /工作/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189) UnityEditor.GenericInspector.OnOptimizedInspectorGUI(矩形 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) UnityEditor.InspectorWindow.OnGUI()(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:266) System.Reflection.MonoMethod.Invoke(System.Object的OBJ, BindingFlags invokeAttr,System.Reflection.Binder聯編程序,System.Object [] parameters,System.Globalization.CultureInfo culture)(在 /Applications/builderAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod。 cs:222)
嘗試改變'[CustomPropertyDrawer(typeof運算(ScaledCurve))]''''到'[CustomPropertyDrawer(typeof(SimpleClass))]'在你的SimpleClassDrawer.cs文件中,並可能改變SimpleClassDr aware子類PropertyDrawer? – Jerdak
根據示例,SimpleClassDrawer需要擴展PropertyDrawer? – lukegravitt