Created
March 2, 2018 10:14
-
-
Save balaam/8c4bfe0c716bda03222c7669f5dcf905 to your computer and use it in GitHub Desktop.
Instance Helper for Scriptable Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using UnityEngine; | |
using UnityEditor; | |
public class ScriptableObjectInstanceHelper | |
{ | |
[MenuItem ("Assets/Create Instance", false, 0)] | |
private static void CreateInstance () | |
{ | |
bool shouldSave = false; | |
ScriptableObject lastAssetCreated = null; | |
foreach (string id in Selection.assetGUIDs) | |
{ | |
string path = AssetDatabase.GUIDToAssetPath (id); | |
if (AssetDatabase.GetMainAssetTypeAtPath (path) == typeof(MonoScript)) | |
{ | |
MonoScript script = (MonoScript)AssetDatabase.LoadAssetAtPath (path, typeof(MonoScript)); | |
if (script.GetClass ().IsSubclassOf (typeof(ScriptableObject))) | |
{ | |
ScriptableObject asset = ScriptableObject.CreateInstance (script.GetClass ()); | |
string outputPath = string.Format("{0}/{1}_instance.asset", | |
System.IO.Path.GetDirectoryName (path), | |
string.Format(script.GetClass().Name)); | |
outputPath = AssetDatabase.GenerateUniqueAssetPath (outputPath); | |
shouldSave = true; | |
AssetDatabase.CreateAsset(asset, outputPath); | |
} | |
} | |
if (shouldSave) { | |
AssetDatabase.SaveAssets (); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = lastAssetCreated; | |
} | |
} | |
} | |
[MenuItem ("Assets/Create Instance", true)] | |
private static bool CreateInstanceVerify() | |
{ | |
foreach (string id in Selection.assetGUIDs) { | |
string path = AssetDatabase.GUIDToAssetPath (id); | |
if (AssetDatabase.GetMainAssetTypeAtPath (path) == typeof(MonoScript)) { | |
MonoScript script = (MonoScript)AssetDatabase.LoadAssetAtPath (path, typeof(MonoScript)); | |
if (script.GetClass ().IsSubclassOf (typeof(ScriptableObject))) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment