- Home /
Can't use DecoratorDrawer with ScriptableObject in Unity
I asked this on stackoverflow and even though I got a good answer, it's not ideal and it's sort of a hack.
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?
One thing that is worth pointing out is that the label can be multi line. I basically want a [Tooltip] that is just there, no mouseover to see it.
If you cross post, please include a link in your question to your crosspost on SO.
Answer by Bunny83 · Mar 18, 2018 at 07:58 PM
PropertyDrawers as well sa DecoratorDrawers can't use GUILayout functions. You have to use the GUI / EditorGUI methods. Your OnGUI callback gets a Rect parameter which you have to use to draw your stuff. If you need more space you also need to override the GetHeight method and return the size you need.
ps: If you just want to display some text above a property, just use the Header attribute. It's a predefined decorator which does exactly this.
I understand what you're saying, but is there documentation that states this? The drawing of properties should be generic, regardless of where it is. If it's not, then unity editor coding is even worse than I thought.