I wrote a simple (abstract) PropertyDrawrer class for ScriptableObjects.
The following version of the PropertyDrawrer simply draws the ScriptableObject as BOTH an Editor “ObjectField”, followed by the internal members of the ScriptableObject itself.
Abstract PropertyDrawer Class:
[CustomPropertyDrawer(typeof(ScriptableObject))]
public abstract class ScriptableObjectPropertyDrawer : PropertyDrawer
{
Editor editorWindow = null;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUI.ObjectField(position, property, this.fieldInfo.FieldType, label);
Editor.CreateCachedEditor(property.objectReferenceValue, null, ref editorWindow);
editorWindow.DrawDefaultInspector();
EditorGUI.EndProperty();
}
}
Sample ScriptableObject class: Settings
public class Settings : ScriptableObject {
public int intValue;
public string stringValue;}
Sample Monobehavior that CONTAINS the sample scriptable object: MonoTest
public class MonoTest : MonoBehaviour {
public Settings s; }
Explicit definition of the PropertyDrawer for our Settings scriptable object. This tells the system to use our custom property drawer for ALL “Settings”.
[CustomPropertyDrawer(typeof(Settings))]
public class SettingsPropertyDrawer : ScriptableObjectPropertyDrawer {}
Now as far as I understand things, this SHOULD be all that is necessary. Default inspectors should now use the newly defined SettingsPropertyDrawer , when drawing any “Settings”. However, compiling this code, and then selecting/inspecting a MonoTest object in the Editor, yields the following error:
ArgumentException: Getting control 9’s position in a group with only 9 controls when doing Repaint
Aborting
The call Stack shows that the line in MY code generating the error is the line…
editorWindow.DrawDefaultInspector();
Though the actual error is generated by internal unity code:
UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUILayoutUtility.cs:637)
Now, I HAVE seen a bunch of posts on this error, but they all have to do with: changing what controls are being drawn, at the wrong time.
Since I have no IF statements in my code, it’s pretty obvious that changing controls is NOT the issue.
RESOLUTION: Oddly, adding in THIS do-nothing class resolved the error.
[CustomEditor(typeof(MonoTest))]
public class MonoTestEditor : Editor{}
Questions:
Why does this resolve it?
What can I do so that users of the PropertyDrawer class do NOT need to define a custom editor for their monobehavior, like the resolution above.
I doubt this is a unity bug, where am I making wrong assumptions?