Hi, guys,
so, I think a lot of you have been using ScriptableObjects with Unity. I myself have been using them not only for things like items or other types of asset databases, but also for ways to store gameplay relevant stats that can easily be edited by designers and makes rapid iteration even easier. One thing that crops up a lot in our production are scriptables with a few parameters that define members like “playerHealth”, “playerDamage” or things like that. Jumping from the player inspector to the scriptableObject, changing the property, jumping back to the player, maybe accessing another scriptable with movement data, changing this object, testing the changes, jumping back to the various scriptables, making alterations, iterating, and so on. It can get quite tiring, altering multiple scriptables at once.
One thing that would help a lot, would be to display the scriptables member variables within the components inspector gui. And since we found no easy accessible solution for that, we made one ourselves.
Our two simple editor scripts turn what would previously be found in two inspectors
(Player Object)
(Scriptable Object)
Into an easy to manage and edit, single foldout inspector:
(Player and Scriptable Object combined)
If any of you want to try it out or offer feedback, please check out the code below.
Simply place those two scripts inside the editor folder:
ScriptableObjectDrawer.cs
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(ScriptableObject), true)]
public class ScriptableObjectDrawer : PropertyDrawer
{
// Cached scriptable object editor
private Editor editor = null;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Draw label
EditorGUI.PropertyField(position, property, label, true);
// Draw foldout arrow
if (property.objectReferenceValue != null)
{
property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, GUIContent.none);
}
// Draw foldout properties
if (property.isExpanded)
{
// Make child fields be indented
EditorGUI.indentLevel++;
// Draw object properties
if (!editor)
Editor.CreateCachedEditor(property.objectReferenceValue, null, ref editor);
editor.OnInspectorGUI();
// Set indent back to what it was
EditorGUI.indentLevel--;
}
}
}
MonoBehaviourEditor.cs
using UnityEngine;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor(typeof(MonoBehaviour), true)]
public class MonoBehaviourEditor : Editor
{
}
This one is mandatory since without it, the custom property drawer will throw errors. You need a custom editor class of the component utilising a ScriptableObject. So we just create a dummy editor, that can be used for every MonoBehaviour. With this empty implementation it doesn’t alter anything, it just removes Unitys property drawing bug.
Please try it out, I’m open for feedback and suggestions. Other than that, I hope this was useful, and enjoy!
Edit: For easier inporting, here is a link to a unitypackage


