gpt4 book ai didi

c# - SerializedProperty 在 Unity3D PropertyDrawers 中始终为 null

转载 作者:行者123 更新时间:2023-11-30 12:10:35 25 4
gpt4 key购买 nike

我第一次尝试使用 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 公共(public)字段的 MonoBehavior:

public class Test : MonoBehaviour {
public SimpleClass s;
}

SimpleClassDrawerOnGUI方法总是被调用,但是myField总是为null。

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

编辑:

经过更深入的调查,OnGUI 似乎被调用了两次。第二次该属性为 null 并在我尝试绘制它时抛出 NullReferenceExceptionwEditorGUI.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 (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 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) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (at 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) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/MonoGenerated/Editor/EditorGUI.cs:3688) PropertyDrawerTest.OnGUI (Rect pos, UnityEditor.SerializedProperty prop, UnityEngine.GUIContent label) (at 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, 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) (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[] parameters, System.Globalization.CultureInfo culture) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.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;
}
}

我包装的属性(property):

using UnityEngine;
using System.Collections;

[System.Serializable]
public class ScaledCurve {
public int myValue = 1;
}

以及使用此属性的类:

public class PropertyDrawerImpl : MonoBehaviour {
public ScaledCurve Curve;
}

关于c# - SerializedProperty 在 Unity3D PropertyDrawers 中始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com