Question

I'm trying ot make a custom drawer for a simple Aspect:ScriptableObject. I can get everything to work for the Aspect class as long as it's a generic class, but as soon as it's a ScriptableObject everything breaks. Specifically:

EditorGUI.PropertyField(position, property.FindPropertyRelative("AspectName"),
                        new GUIContent("Aspect:"));

but the findPropRel is null. using property.serializedObject returns the parent object the Aspect is on, not the Aspect. I can't even hack together a solution that uses that since I don't know which aspect it is.

Is there no way to create a customDrawer for scriptableObjects or Monobehaviours? I can't imagine Unity would make CustomDrawers that can't even be used with the most common Unity class.

And no, a customEditor really doesn't work in this situation. I'd have to make dozens for completely unrelated classes just to draw the aspect.

No correct solution

OTHER TIPS

I am also having the same problem. I've reached a kind of trick to get properties from a ScriptableObject, but not using FindPropertyRelative, wich does not work for ScriptableObject but works well for plain objects.

Also using MonoBehaviour will not work if you plan to instantiate the class in another MonoBehavior class, as MonoBehavior classes can only be added as components to GameObject(s). You cannot create it using new, or Unity will complain.

I have to tell you, that yet I don't think my solution is really valid because I am not sure if it works well on prefabs, but that could be maybe because I am trying to save some scene references on the prefab and that seems that cannot be done in Unity.

Well. Stop speech. That is what I do:

Supposing your ScriptabeObject you want to access is of class MySOClass.

public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
    // Make a new serialized object for your property that is a class that inherits
    // from ScriptableObject
    SerializedObject serializedObject = new SerializedObject(prop.objectReferenceValue as MySOClass);
    SerializedProperty propSP = serializedObject.FindProperty("prop");

    if (propSP != null) {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(propSP , true);
        if (EditorGUI.EndChangeCheck()) {
            serializedObject.ApplyModifiedProperties();
        }
    }
}

The fact that I test nulliness of propSP , is because if the actual value of prop is null, Serialized property returned will be also null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top