SerializedProperty在Unity3D PropertyDrawers中始终为null

时间:2013-07-30 16:01:11

标签: c# unity3d

我第一次尝试使用Unity3D的PropertyDrawer

我完全复制并粘贴了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
    }

最后是一个公共字段为SimpleClass的MonoBehavior:

public class Test : MonoBehaviour {
  public SimpleClass s;
}

始终调用OnGUI SimpleClassDrawer方法,但myField始终为空。

我不明白我错过了什么?任何线索?

编辑:

经过深入调查后,似乎OnGUI被称为两次。第二次该属性为null并在我尝试绘制它时抛出NullReferenceExceptionw EditorGUI.PropertyField

EDIT2:

我帖子的最后一个版本包含了一些复制和粘贴错误。我甚至尝试使用Jerdak发布的代码,但问题仍然存在于Unity 4.2中。这是堆栈跟踪:

  

NullReferenceException:Curve:SerializedProperty为null   UnityEditor.EditorGUI.BeginProperty(Rect totalPosition,   UnityEngine.GUIContent标签,UnityEditor.SerializedProperty属性)   (在   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3278)   UnityEditor.EditorGUI.SinglePropertyField(Rect position,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签)   (在   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3760)   UnityEditor.EditorGUI.PropertyField(Rect位置,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签,   Boolean includeChildren)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)   UnityEditor.EditorGUI.PropertyField(Rect位置,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签)   (在   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688)   PropertyDrawerTest.OnGUI(Rect pos,UnityEditor.SerializedProperty   prop,UnityEngine.GUIContent label)(at   资产/脚本/编辑/核心/游泳池/ 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(Rect位置,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签,   Boolean includeChildren)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)   UnityEditor.EditorGUI.PropertyField(Rect位置,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签)   (在   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688)   PropertyDrawerTest.OnGUI(Rect pos,UnityEditor.SerializedProperty   prop,UnityEngine.GUIContent label)(at   资产/脚本/编辑/核心/游泳池/ 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(Rect位置,   UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签,   Boolean includeChildren)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)   UnityEditor.EditorGUI.PropertyField(Rect位置,   UnityEditor.SerializedProperty属性,布尔includeChildren)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3683)   UnityEditor.EditorGUI.PropertyField(Rect位置,   UnityEditor.SerializedProperty属性)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3679)   UnityEditor.Editor.OptimizedInspectorGUIImplementation(Rect   contentRect)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189)   UnityEditor.GenericInspector.OnOptimizedInspectorGUI(Rect   contentRect)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/GenericInspector.cs:46)   UnityEditor.InspectorWindow.DrawEditors(Boolean isRepaintEvent,   UnityEditor.Editor [] editors,Boolean eyeDropperDirty)(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:864)   UnityEditor.InspectorWindow.OnGUI()(at   C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:266)   System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags   invokeAttr,System.Reflection.Binder binder,System.Object []   参数,System.Globalization.CultureInfo文化)(at   /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

1 个答案:

答案 0 :(得分: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;
}