4

我第一次尝试使用 Unity3D 的PropertyDrawer

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

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

//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方法SimpleClassDrawer总是被调用,但myField总是为空。

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

编辑:

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

编辑2:

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

NullReferenceException: Curve: SerializedProperty is null UnityEditor.EditorGUI.BeginProperty (Rect totalPosition, UnityEngine.GUIContent label, UnityEditor.SerializedProperty property) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3278) UnityEditor .EditorGUI.SinglePropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3760) UnityEditor.EditorGUI.PropertyField(矩形位置, UnityEditor.SerializedProperty 属性,UnityEngine.GUIContent 标签,布尔值 includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694) UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,统一引擎。GUIContent 标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (在 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/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor。 EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,UnityEngine。GUIContent 标签) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (在 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/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor。 EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性,Boolean includeChildren)(在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3683)UnityEditor.EditorGUI.PropertyField(矩形位置,UnityEditor.SerializedProperty 属性)(在 C:/BuildAgent/work/cac08d8a5e25d4cb /Editor/MonoGenerated/Editor/EditorGUI.cs:3679) UnityEditor.Editor.OptimizedInspectorGUIImplementation (Rect contentRect) (在 C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorBindings.cs:189) UnityEditor.GenericInspector.OnOptimizedInspectorGUI (Rect 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章 : Reflection.Binder binder,System.Object[] 参数,System.Globalization.CultureInfo 文化)(位于 /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

4

1 回答 1

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;
}
于 2013-07-30T18:27:41.080 回答