Creating ScriptableObjects in Unity

This is a short editor script that I use for creating instances of ScriptableObject classes in the Unity editor. Most scripts that create ScriptableObjects require you to add new code for each object type. This script does not. Select any class that extends ScriptableObject and choose Create->Instance to create an instance of that class.

using UnityEngine;
using UnityEditor;
using System.IO;

public static class ScriptableObjectCreator {
  [MenuItem( "Assets/Create/Instance" )]
  public static void CreateInstance() {
    foreach( Object o in Selection.objects ) {
      if( o is MonoScript ) {
        MonoScript script = (MonoScript)o;
        System.Type type = script.GetClass();
        if( type.IsSubclassOf( typeof( ScriptableObject ) ) ) {
          CreateAsset( type );
        }
      }
    }
  }

  [MenuItem( "Assets/Create/Instance", true )]
  public static bool ValidateCreateInstance() {
    foreach( Object o in Selection.objects ) {
      if( o is MonoScript ) {
        MonoScript script = (MonoScript)o;
        System.Type type = script.GetClass();
        if( type.IsSubclassOf( typeof( ScriptableObject ) ) ) {
          return true;
        }
      }
    }
    return false;
  }

  private static void CreateAsset( System.Type type ) {
    var asset = ScriptableObject.CreateInstance( type );
    string path = AssetDatabase.GetAssetPath( Selection.activeObject );
    if( path == "" )  {
      path = "Assets";
    } else if( Path.GetExtension( path ) != "" ) {
      path = path.Replace( Path.GetFileName( AssetDatabase.GetAssetPath( Selection.activeObject ) ), "" );
    }
    string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath( path + "/New " + type.ToString() + ".asset" );
    AssetDatabase.CreateAsset( asset, assetPathAndName );
    AssetDatabase.SaveAssets();
    EditorUtility.FocusProjectWindow();
    Selection.activeObject = asset;
  }
}

To use the script, just add it to a folder called “Editor” inside your Unity assets folder.

The script isn’t as sophisticated as ScriptableObject Factory but I prefer it because it is quicker to use – just right click on a class and choose Create->Instance.

This entry was tagged .

One thought on “Creating ScriptableObjects in Unity

  1. Nice to see so many approaches to this :)

    I have blogged about this in the past here: http://www.tallior.com/unity-scriptableobject-factory/

    My approach displays a small window with a dropdown that lists all the different ScriptableObject-derived types in the project.

    I should probably update that to actually look a bit better, etc.

    There’s also a new attribute added in one of the latest versions of Unity – http://docs.unity3d.com/ScriptReference/CreateAssetMenuAttribute.html

    This marks a ScriptableObject type so that it will be listed under the Assets/Create menu. The creation part is handled by Unity in this case.

    I actually played around with the idea to create some crazy editor code that modifies the compiled assemblies using Mono.Cecil or something to actually add that attribute automatically to all ScriptableObjects types :)

Leave a Reply

Your email address will not be published. Required fields are marked *