Skip to content

Instantly share code, notes, and snippets.

@prodigga
Last active May 13, 2018 07:06
Show Gist options
  • Save prodigga/0d68a1471936c69b9f0d to your computer and use it in GitHub Desktop.
Save prodigga/0d68a1471936c69b9f0d to your computer and use it in GitHub Desktop.
Right click on any script in your project that defines a ScriptableObject and create a ScriptableObject asset out of it! See here http://imgur.com/a/e7FDR#0 NOTE: This script was made somewhat redundant with the introduction of the [CreateAssetMenu] attribute - look it up :)
using UnityEditor;
using UnityEngine;
/// <summary>
/// Right click on any script in your project that defines a ScriptableObject
/// and create a ScriptableObject asset out of it! See here http://imgur.com/a/e7FDR#0
/// </summary>
/// <author>Tim Aksu</author>
/// <email>timur.s.aksu@gmail.com</email>
public static class ScriptableObjectUtility
{
[MenuItem("Assets/Create/Asset From Selected Script")]
public static void CreateAssetFromSelectedScript()
{
MonoScript ms = Selection.objects[0] as MonoScript;
string path = EditorUtility.SaveFilePanel("Save location", "Assets", "New " + ms.name, "asset");
if ( string.IsNullOrEmpty(path) )
return;
//Get project relative path and ensure path is within project
var projectRelative = FileUtil.GetProjectRelativePath(path);
if (string.IsNullOrEmpty(projectRelative))
{
EditorUtility.DisplayDialog("Error", "Please select somewhere within your assets folder.", "OK");
return;
}
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(projectRelative);
ScriptableObject scriptableObject = ScriptableObject.CreateInstance(ms.GetClass());
AssetDatabase.CreateAsset(scriptableObject, assetPathAndName);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = scriptableObject;
}
[MenuItem("Assets/Create/Asset From Selected Script", true)]
public static bool CreateAssetFromSelectedScript_Validator()
{
if ( Selection.objects != null &&
Selection.objects.Length == 1 &&
Selection.objects[0] is MonoScript &&
(Selection.objects[0] as MonoScript).GetClass().IsSubclassOf(typeof(ScriptableObject))&&
!(Selection.objects[0] as MonoScript).GetClass().IsSubclassOf(typeof(UnityEditor.Editor))
)
{
return true;
}
else
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment