Over the course of development, I’m constantly finding cool features of Unity and c# that I never new existed. I’ve found some that are really useful, so I’m going to start sharing them regularly here. This week’s requires some prior knowledge. Definitely read up on ScriptableObjects and custom editors if you are unfamiliar. Without further ado:

 

Making a inspector for any ScriptableObject

Did you know any class deriving from ScriptableObject can be serialized and rendered in a custom Editor with a just a few lines of code? As it turns out, it is really easy:

 

  1. // Creating an instance of a ScriptableObject
  2. MyScriptableObject so = ScriptableObject.CreateInstance();
  3.  
  4. // Setting up the editor
  5. Editor myEditor = Editor.CreateEditor(so);
  6.  
  7. // Drawing the editor
  8. myEditor.OnInspectorGUI();

And that’s pretty much (almost) it! Some stuff you should know:

  • In this example, myEditor.OnInspectorGUI() must be called in your EditorWindow or custom Editor’s OnInspectorGUI() method.
  • Editor.CreateEditor() only needs to be called once, so you can absolutely call it once and cache the result. However, there is a catch: Editor.CreateEditor() throws an error when you call it from OnEnable. You will have to call it in your editor’s OnInspectorGUI() method, and if you want to be efficient you’ll have to use a boolean to make sure it is only called once.

Below is a full code example. In it, we’ll instance a ScriptableObject in a Monobehaviour. We’ll then write a custom inspector that will display the variables in the Monobehaviour, including those in the ScriptableObject:

ExampleScriptableObject.cs

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [System.Serializable]
  5. public class ExampleScriptableObject : ScriptableObject {
  6.     // Some variables we also want displayed in the inspector
  7.     public float scriptableObjectFloat = 42f;
  8.     public int scriptableObjectInt = 42;
  9. }

This will be the ScriptableObject we want to display in the inspector. It has two variables, scriptableObjectFloat and scriptableObjectInt, that will display in the inspector once we get the rest of our code working.

 

ExampleMonobehaviour.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. public class ExampleMonobehaviour : MonoBehaviour {
  4.  
  5.     // Example variable that will display in the inspector
  6.     public string exampleMonobehaviourString = "foo";
  7.  
  8.     /* The scriptable object we want to display in the inspector. Note the [HideInInspector] tag; 
  9.      we don't actually need Unity to display the ScriptableObject, our code will handle that.*/
  10.     [HideInInspector]
  11.     public ExampleScriptableObject exampleScriptableObject = ScriptableObject.CreateInstance();
  12.  
  13. }

This is the Monobehaviour that we will build a custom inspector for. It has a variable called “exampleMonobehaviourString” that will be displayed in the inspector alongside the variables contained in the ScriptableObject we made, which is instanced in this class as “exampleScriptableObject”.

 

ExampleMonobehaviourEditor.cs

 

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. [CustomEditor(typeof(ExampleMonobehaviour))]
  6. public class ExampleMonobehaviourEditor : Editor {
  7.  
  8.     // We'll cache the editor here
  9.     private Editor cachedEditor;
  10.  
  11.     /* using this boolean to keep track if whether cachedEditor has already been assigned too.
  12.      We're required to call Editor.CreateEditor() from OnInspectorGUI(), which is called often, 
  13.      but we only really need to call Editor.CreateEditor() once. */
  14.     private bool cachedEditorNeedsRefresh = true;
  15.  
  16.     public void OnEnable() {
  17.         // Resetting cachedEditor, and marking it to be reassigned
  18.         cachedEditor = null;
  19.         cachedEditorNeedsRefresh = true;
  20.     }
  21.  
  22.  
  23.     public override void OnInspectorGUI() {
  24.         // Grabbing the object this inspector is editing.
  25.         ExampleMonobehaviour editedMonobehaviour = (ExampleMonobehaviour)target;
  26.  
  27.         //Checking if we need to get our Editor. Calling Editor.CreateEditor() if needed
  28.         if (cachedEditorNeedsRefresh) {
  29.             cachedEditor = Editor.CreateEditor(editedMonobehaviour.exampleScriptableObject);
  30.  
  31.             //Ensuring this is only run once.
  32.             cachedEditorNeedsRefresh = false;
  33.         }
  34.  
  35.         /* We want to show the other variables in our Monobehaviour as well, so we'll call
  36.          the superclasses' OnInspectorGUI(). Note this could also be accomplished by a call
  37.          to DrawDefaultInspector() */
  38.         base.OnInspectorGUI();
  39.  
  40.         //Drawing our ScriptableObjects inspector
  41.         cachedEditor.DrawDefaultInspector();
  42.     }
  43. }

Finally, the class that does the work! This class caches an editor in line 29 and draws it in line 41. This displays our ScriptableObject’s variables alongside those of ExampleMonobehaviour. It’s surprisingly useful! Here’s what it looks like in practice:

 

Very nice, isn't it! Monobehaviour variables and ScriptableObject variables living side-by-side...

Very nice, isn’t it! Monobehaviour variables and ScriptableObject variables all displayed neatly in the inspector.

 

And that’s it! Leave your questions and comments down below!

Cheers,

-Mark