Search code examples
c#unity-game-engineunity-editor

Can't use DecoratorDrawer with ScriptableObject in Unity


I think I might be missing something very obvious here, but heres goes a boiled down version of the problem I'm having.

I have this game object script:

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

This TestAttribute.cs class

public class TestAttribute : PropertyAttribute {}

And this DecoratorDrawer extension class:

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

This works great!

But if I try to put the exact same thing in a ScriptableObject instance like this:

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

It won't do it and gives me this error:

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)

Can anyone tell me whats wrong?


Solution

  • I don't know why but i can show you a way to solve it:

    [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");
        }
    }
    

    Edited

    I hope this code can help you.

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

    And drawer:

    [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);
        }
    }
    

    now:

    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;
    }