Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2015 12:07
Show Gist options
  • Save anonymous/d2575f36b144392bdc14 to your computer and use it in GitHub Desktop.
Save anonymous/d2575f36b144392bdc14 to your computer and use it in GitHub Desktop.
Add a create instance button on the inspector of scripts that can generate a ScriptableObject
using UnityEditor;
using UnityEngine;
// From https://www.youtube.com/watch?v=SyR4OYZpVqQ
[CustomEditor(typeof(MonoScript))]
public class ScriptInspector : Editor
{
public override void OnInspectorGUI()
{
MonoScript monoScript = target as MonoScript;
System.Type type = monoScript.GetClass();
if (type != null &&
type.IsSubclassOf(typeof(ScriptableObject)) &&
!type.IsSubclassOf(typeof(UnityEditor.Editor)))
{
if (GUILayout.Button("Create Instance", GUILayout.Height(30)))
{
ScriptableObject asset = ScriptableObject.CreateInstance(type);
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/" + type.Name + ".asset");
AssetDatabase.CreateAsset(asset, path);
EditorGUIUtility.PingObject(asset);
}
GUILayout.Space(10);
}
GUILayout.Label(monoScript.text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment