繁体   English   中英

SerializedProperty在Unity3D PropertyDrawers中始终为null

[英]SerializedProperty always null with Unity3D PropertyDrawers

我正在尝试第一次使用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
    }

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

public class Test : MonoBehaviour {
  public SimpleClass s;
}

始终调用SimpleClassDrawerOnGUI方法,但myField始终为null。

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

编辑:

经过深入调查后,似乎OnGUI被调用了两次 第二次该属性为null并且每当我尝试通过EditorGUI.PropertyField绘制它时抛出NullReferenceExceptionw

EDIT2:

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

NullReferenceException:Curve:SerializedProperty为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(Rect position,UnityEditor.SerializedProperty property,UnityEngine.GUIContent label)(at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3760)UnityEditor.EditorGUI.PropertyField(Rect position, UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签,Boolean includeChildren)(在C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor.EditorGUI.PropertyField(Rect position,UnityEditor.SerializedProperty property, UnityEngine.GUIContent label)(在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(Rect position,UnityEditor.SerializedProperty property,UnityEngine.GUIContent label)(at C:/ BuildAgent) /work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3746)UnityEditor.EditorGUI.PropertyField(Rect position,UnityEditor.SerializedProperty property,UnityEngine.GUIContent label,Boolean includeChildren)(at C:/ BuildAgent / work / cac08d8a5e25d4cb / Editor / MonoGenerated / Editor / EditorGUI.cs:3694)UnityEditor.EditorGUI.PropertyField(Rect position,UnityEditor.SerializedProperty property,UnityEngine.GUIContent label)(在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(Rect position,UnityEditor。 Seriali zedProperty属性,UnityEngine.GUIContent标签)(在C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3746)UnityEditor.EditorGUI.PropertyField(Rect位置,UnityEditor.SerializedProperty属性,UnityEngine.GUIContent标签, Boolean includeChildren)(在C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3694)UnityEditor.EditorGUI.PropertyField(Rect position,UnityEditor.SerializedProperty property,Boolean includeChildren)(at C:/ BuildAgent / work / cac08d8a5e25d4cb / Editor / MonoGenerated / Editor / EditorGUI.cs:3683)UnityEditor.EditorGUI.PropertyField(Rect position,UnityEditor.SerializedProperty property)(at 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)(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()(在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 [] parameters,System.Globalization.CultureInfo culture)(at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection /MonoMethod.cs:222)

这是一个工作示例,Unity版本4.1.3f3。 我不确定你的问题是否与SimpleClassDrawer有关,而不是使用不正确的类型CustomPropertyDrawer 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM