在Unity中不能将DecoratorDrawer与ScriptableObject一起使用

时间:2018-03-10 00:52:17

标签: c# unity3d unity-editor

我想我可能会遗漏一些非常明显的东西,但是对于我正在解决的问题,这是一个简单的版本。

我有这个游戏对象脚本:

public class PingPong : MonoBehaviour {
    [Test]
    public int value1 = 10;
    [Test]
    public int value2 = 10;
}

此TestAttribute.cs类

public class TestAttribute : PropertyAttribute {}

这个DecoratorDrawer扩展类:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : DecoratorDrawer {
    public override void OnGUI(Rect position) {
        base.OnGUI(position);
        EditorGUILayout.LabelField("Hi", "hello");
    }
}

这很棒!

但是如果我尝试将完全相同的东西放在像这样的ScriptableObject实例中:

[CreateAssetMenu(fileName = "Example", menuName = "Example/Settings", order = 1)]
public class SpecialSettings : ScriptableObject {
    [Test]
    public int value1 = 10;
    [Test]
    public int value2 = 10;
}

它不会这样做并给我这个错误:

ArgumentException: Getting control 4's position in a group with only 4 controls when doing Repaint
Aborting
UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/Modules/IMGUI/LayoutGroup.cs:117)
UnityEngine.GUILayoutUtility.DoGetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Modules/IMGUI/GUILayoutUtility.cs:444)
UnityEngine.GUILayoutUtility.GetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Modules/IMGUI/GUILayoutUtility.cs:404)
UnityEditor.InspectorWindow.CheckDragAndDrop (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:622)
UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:443)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:291)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:284)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:251)

谁能告诉我什么错了?

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但我可以告诉你解决问题的方法:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label) * 2;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        position.height /= 2;
        EditorGUI.PropertyField(position, property, label);
        position.y += EditorGUIUtility.singleLineHeight;
        EditorGUI.LabelField(position, "hello", "hi");
    }
}

<强>被修改

我希望这段代码可以帮到你。

public class TestAttribute : PropertyAttribute
{
    public string content;
    public int maxlineCount;

    private TestAttribute() { }
    public TestAttribute(string content, int maxlineCount)
    {
        this.content = content;
        this.maxlineCount = maxlineCount;
    }
}

抽屉:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        var lineCount = ((TestAttribute) attribute).maxlineCount + 1;
        return (base.GetPropertyHeight(property, label) + EditorGUIUtility.standardVerticalSpacing) * lineCount;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var ta = (TestAttribute) attribute;

        position.height = EditorGUIUtility.singleLineHeight;
        EditorGUI.PropertyField(position, property, label);

        position.height *= ta.maxlineCount;
        position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
        position = EditorGUI.PrefixLabel(position, new GUIContent("About"));

        EditorGUI.TextArea(position, ta.content, EditorStyles.wordWrappedLabel);
    }
}

现在:

public class PingPong : MonoBehaviour
{
    [Test("some single line text here.", 1)]
    public int value1 = 10;
    [Test("some other text for two lines and with no meaning.", 2)]
    public int value2 = 10;
}