2013-07-30 80 views
2

我想第一次使用Unity3D的PropertyDrawerSerializedProperty始終爲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)

+0

嘗試改變'[CustomPropertyDrawer(typeof運算(ScaledCurve))]''''到'[CustomPropertyDrawer(typeof(SimpleClass))]'在你的SimpleClassDrawer.cs文件中,並可能改變SimpleClassDr aware子類PropertyDrawer? – Jerdak

+0

根據示例,SimpleClassDrawer需要擴展PropertyDrawer? – lukegravitt

回答

3

下面是一個工作示例,Unity版本4.1.3f3。我不確定您的問題是否與使用不正確類型的SimpleClassDrawer沒有繼承PropertyDrawerCustomPropertyDrawer有關。 (正如我在我的評論中指出的那樣。)

的屬性包裝:

using UnityEngine; 
using UnityEditor; 
using System.Collections; 


[CustomPropertyDrawer (typeof (ScaledCurve))] 
public class PropertyDrawerTest : PropertyDrawer { 
public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label) { 
    SerializedProperty myValue = prop.FindPropertyRelative ("myValue"); 

    int indent = EditorGUI.indentLevel; 
    EditorGUI.indentLevel = 1; 
    EditorGUI.PropertyField(
     new Rect(pos.x,pos.y,pos.width,pos.height), 
     myValue, 
     label 
    ); 
    EditorGUI.indentLevel = indent; 
} 
} 

我包裹的屬性:

using UnityEngine; 
using System.Collections; 

[System.Serializable] 
public class ScaledCurve { 
    public int myValue = 1; 
} 

並使用該屬性的類:

public class PropertyDrawerImpl : MonoBehaviour { 
    public ScaledCurve Curve; 
} 
+0

是的你是對的,但它只是一個複製和粘貼錯誤。我正在使用unity 4.2的最後一個版本,它似乎不起作用。你正在使用哪個版本? – Heisenbug

+0

版本4.1.3f3。 [This might](http://forum.unity3d.com/threads/192135-Unity-4-2-and-Property-Drawers)有關係。 – Jerdak

+0

並在版本4.2.0f4中確認爲問題。 – Jerdak