Scriptable Object Custom Inspector Woes

I am trying to create a custom inspector for Scriptable Objects like the Adv Hacking presentation did in Unite 2014.

I am ran into two issues, first here is the script I have:

using UnityEditor;
using UnityEngine;

[CustomEditor (typeof(MonoScript))]
public class ScriptInspector : Editor
{
    public override void OnInspectorGUI()
    {
       
        MonoScript ms = target as MonoScript;
        System.Type type = ms.GetClass();
        if (type != null &&
            type.IsSubclassOf(typeof(ScriptableObject)) &&
            !type.IsSubclassOf(typeof(UnityEditor.Editor)))
        {
            if (GUILayout.Button("Create Instance"))
            {
                ScriptableObject asset = ScriptableObject.CreateInstance(type);
                string path = AssetDatabase.GenerateUniqueAssetPath("Assets/" + type.Name + ".asset");
                AssetDatabase.CreateAsset(asset, path);
                EditorGUIUtility.PingObject(asset);
            }
        }
     }
}

The first problem is, I get this error (twice) when I drag the script into Editor folder.

Instance of ScriptInspector couldn’t be created. The script class needs to derive from ScriptableObject and be placed in the Assets/Editor folder.

The script works fine, I don’t see any problems, but I would like to know why I am getting this error and what I am possibly doing wrong to cause it.

Second issue, this script overrides the default inspector for Script Files. If it detects a Scriptable Object, it creates a button to make it easy to create an instance of the object without manually doing it in a temporary script. The problem is, if the script is not a scriptable object, it removes the default functionality of showing the source code of the file. I tried using else { DrawDefaultInspector(); } but that just shows something totally different and not the source code.

This is what is shown if you click on non Scriptable Object in inspector and use DrawDefaultInspector();

Try this :wink:

using UnityEngine;
using UnityEditor;

[CustomEditor (typeof(MonoScript))]
public class ScriptInspectorTest : Editor
{
    private Editor defaultEditor;
    private static System.Type defaultInspectorType;

    public override void OnInspectorGUI()
    { 
        MonoScript ms = target as MonoScript;
        System.Type type = ms.GetClass();
        if (type != null &&
            type.IsSubclassOf(typeof(ScriptableObject)) &&
            !type.IsSubclassOf(typeof(UnityEditor.Editor)))
        {
            if (GUILayout.Button("Create Instance"))
            {
                ScriptableObject asset = ScriptableObject.CreateInstance(type);
                string path = AssetDatabase.GenerateUniqueAssetPath("Assets/" + type.Name + ".asset");
                AssetDatabase.CreateAsset(asset, path);
                EditorGUIUtility.PingObject(asset);
            }
        }

        if (defaultEditor == null)
        {
            if (defaultInspectorType == null)
                defaultInspectorType = typeof(Editor).Assembly.GetType("UnityEditor.MonoScriptInspector");
            defaultEditor = Editor.CreateEditor(target, defaultInspectorType);
        }
        if (!(defaultEditor is ScriptInspectorTest))
            defaultEditor.OnInspectorGUI();
     }
}

I ended up resolving it, but thank you.

So, what was the problem, and how did you resolve it?

If I remember correct, the error was pointless, it just happens when you copy it over but does not cause any problems. I ended up going a different route in the end.