C# Unity3D PropertyDrawers的SerializedProperty始终为空

C# Unity3D PropertyDrawers的SerializedProperty始终为空,c#,unity3d,C#,Unity3d,我第一次尝试使用Unity3D 我复制并粘贴了中的示例,但它似乎不起作用 以下是一个仍然无法使用的简化版本: //a serializable class, it should be possible write an custom property drawer for it [System.Serializable] public class SimpleClass { public int myField; } 这是一个空抽屉,什么也没画: [CustomPropertyDrawer

我第一次尝试使用Unity3D

我复制并粘贴了中的示例,但它似乎不起作用

以下是一个仍然无法使用的简化版本:

//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
的单行为:

public class Test : MonoBehaviour {
  public SimpleClass s;
}
始终调用
SimpleClassDrawer
OnGUI
方法,但
myField
始终为空

我不明白我错过了什么?有线索吗

编辑:

经过更深入的调查,似乎
OnGUI
被称为两次。第二次,该属性为null,每当我尝试绘制它时,就会抛出一个
NullReferenceExceptionw
EditorGUI.PropertyField

编辑2:

我的文章的最后一个版本包含了一些复制和粘贴错误。我甚至尝试过使用Jerdak发布的代码,但问题至少在Unity 4.2中仍然存在。这是stacktrace:

NullReferenceException:曲线:SerializedProperty为null UnityEditor.EditorGUI.BeginProperty(Rect totalPosition, UnityEngine.gui内容标签,UnityEdit.SerializedProperty属性) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3278) UnityEditor.EditorGUI.SinglePropertyField(矩形位置, UnityEdit.SerializedProperty属性,UnityEngine.GUI内容标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3760) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty属性,UnityEngine.GUI内容标签, 布尔值includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEdit.SerializedProperty属性,UnityEngine.GUI内容标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawertTest.OnGUI(Rect pos,UnityEdit.SerializedProperty 道具,UnityEngine.gui内容标签)(位于 Assets/Scripts/Editor/Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField(矩形位置, UnityEdit.SerializedProperty属性,UnityEngine.GUI内容标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3746) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty属性,UnityEngine.GUI内容标签, 布尔值includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEdit.SerializedProperty属性,UnityEngine.GUI内容标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawertTest.OnGUI(Rect pos,UnityEdit.SerializedProperty 道具,UnityEngine.gui内容标签)(位于 Assets/Scripts/Editor/Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField(矩形位置, UnityEdit.SerializedProperty属性,UnityEngine.GUI内容标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3746) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty属性,UnityEngine.GUI内容标签, 布尔值includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEdit.SerializedProperty属性,布尔值includeChildren)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3683) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEdit.SerializedProperty属性)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3679) UnityEditor.Editor.OPTIMIZEDINSPECTORGUI实现(Rect contentRect)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189) UnityEdit.GenericInspector.OnOptimizedInspectorUI(Rect contentRect)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/genericsinspector.cs:46) UnityEdit.InspectorWindow.DrawEditors(布尔值IsRepaintent, UnityEditor.Editor[]编辑器,布尔滴管器脏)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:864) UnityEdit.InspectorWindow.OnGUI()(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:266) System.Reflection.MonMethod.Invoke(System.Object对象,BindingFlags invokeAttr,System.Reflection.Binder活页夹,System.Object[] 参数,System.Globalization.CultureInfo文化)(位于 /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/monmethod.cs:222)


下面是一个工作示例,Unity版本4.1.3f3。我不确定您的问题是否与使用错误类型的
SimpleClassDrawer
未子类化
PropertyDrawer
CustomPropertyDrawer
有关。(正如我在评论中指出的那样。)

属性包装器:

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;
}

尝试将SimpleClassDrawer.cs文件中的
[CustomPropertyDrawer(typeof(ScaledCurve))]
更改为
[CustomPropertyDrawer(typeof(SimpleClass))]
,或者将SimpleClassDrawer更改为子类PropertyDrawer?根据示例,SimpleClassDrawer需要扩展PropertyDrawer?是的,但这只是一个复制粘贴错误。我使用的是unity 4.2的最新版本,它似乎不起作用。您使用的是哪个版本?版本4.1.3f3。相关。并在版本4.2.0f4中确认为问题。