Unity3D PropertyDrawers 中 SerializedProperty 始终为 null

Hei*_*bug 5 c# unity-game-engine

我第一次尝试使用 Unity3D 的PropertyDrawer

我完全复制并粘贴了博客中找到的示例,但它似乎不起作用。

这是一个仍然不起作用的简化版本:

//a serializable class, it should be possible write an custom property drawer for it
[System.Serializable]
public class SimpleClass
{
  public int myField;
}
Run Code Online (Sandbox Code Playgroud)

这是一个空抽屉,什么也没有画:

[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
    }
Run Code Online (Sandbox Code Playgroud)

最后是具有公共字段的 MonoBehavior SimpleClass

public class Test : MonoBehaviour {
  public SimpleClass s;
}
Run Code Online (Sandbox Code Playgroud)

OnGUI的方法始终SimpleClassDrawer被调用,但myField始终为 null。

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

编辑:

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

编辑2:

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

NullReferenceException:曲线:SerializedProperty为空UnityEditor.EditorGUI.BeginProperty(矩形totalPosition,UnityEngine.GUIContent标签,UnityEditor.SerializedProperty属性)(位于C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerate/Editor/EditorGUI.cs:3278)UnityEditor .EditorGUI.SinglePropertyField(矩形位置,UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签)(位于C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI.cs:3760)UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty 属性、UnityEngine.GUIContent 标签、布尔值 includeChildren)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置、UnityEditor.SerializedProperty 属性、 UnityEngine.GUIContent 标签)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI(矩形 pos、UnityEditor.SerializedProperty prop、UnityEngine.GUIContent 标签)(位于 Assets/Scripts /Editor/Core/Pool/ScaledCurveDrawer.cs:14) UnityEditor.EditorGUI.SinglePropertyField (矩形位置、UnityEditor.SerializedProperty 属性、UnityEngine.GUIContent 标签) (位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI .cs:3746)UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签,布尔includeChildren)(位于C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerate/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签)(位于C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI.cs:3688)PropertyDrawerTest.OnGUI(矩形位置,UnityEditor .SerializedProperty 属性,UnityEngine.GUIContent 标签)(位于 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/MonoGeneerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置、UnityEditor.SerializedProperty 属性、布尔值 includeChildren)(位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI。 cs:3683)UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty属性)(位于C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGeneerated/Editor/EditorGUI.cs:3679)UnityEditor.Editor.OptimizedInspectorGUIImplementation(矩形contentRect) (在C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189) UnityEditor.GenericInspector.OnOptimizedInspectorGUI (矩形 contentRect) (位于 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/GenericInspector.cs:46 ) UnityEditor.InspectorWindow.DrawEditors (布尔 isRepaintEvent、UnityEditor.Editor[] 编辑器、布尔 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[ ] 参数,System.Globalization.CultureInfo 区域性)(位于 /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

Jer*_*dak 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;
}
}
Run Code Online (Sandbox Code Playgroud)

我正在包装的属性:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class ScaledCurve {
    public int myValue = 1;
}
Run Code Online (Sandbox Code Playgroud)

以及使用此属性的类:

public class PropertyDrawerImpl : MonoBehaviour {
    public ScaledCurve Curve;
}
Run Code Online (Sandbox Code Playgroud)