Created
January 26, 2014 20:18
-
-
Save bcatcho/8638881 to your computer and use it in GitHub Desktop.
Revisions
-
Brandon Catcho created this gist
Jan 26, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ ## What it does This script provides a context menu item in the Project window (as well as an extra option in the `Assets>Create` menu) that will instantiate the selected Scriptable object and save it as an asset to a folder of your choosing. ## How to install Throw this script in any folder called `Editor` in your assets folder. ## How to use 1. In the `Project` window, right click on a script that inherits from `ScriptableObject`. The usual context menu will pop up. 2. In the context menu go to `Create` > `Asset from ScriptableObject`. A save file dialog will pop up. 3. In the dialog choose your folder and file name and hit save. 4. Done. 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ using UnityEditor; using UnityEngine; using System; namespace YourCompanyName.Editor { public class ScriptableObjectAssetUtil { [MenuItem("Assets/Create/Asset from ScriptableObject", true)] private static bool CreateScriptableObjAsAssetValidator() { var activeObject = Selection.activeObject; // make sure it is a text asset if ((activeObject == null) || !(activeObject is TextAsset)) { return false; } // make sure it is a persistant asset var assetPath = AssetDatabase.GetAssetPath(activeObject); if (assetPath == null) { return false; } // load the asset as a monoScript var monoScript = (MonoScript)AssetDatabase.LoadAssetAtPath(assetPath, typeof(MonoScript)); if (monoScript == null) { return false; } // get the type and make sure it is a scriptable object var scriptType = monoScript.GetClass(); if (scriptType == null || !scriptType.IsSubclassOf(typeof(ScriptableObject))) { return false; } return true; } [MenuItem("Assets/Create/Asset from ScriptableObject")] private static void CreateScriptableObjectAssetMenuCommand(MenuCommand command) { // we already validated this path, and know these calls are safe var activeObject = Selection.activeObject; var assetPath = AssetDatabase.GetAssetPath(activeObject); var monoScript = (MonoScript)AssetDatabase.LoadAssetAtPath(assetPath, typeof(MonoScript)); var scriptType = monoScript.GetClass(); // ask for a path to save the asset var path = EditorUtility.SaveFilePanelInProject("Save asset as .asset", scriptType.Name + "_asset.asset", "asset", "Please enter a file name"); // catch all exceptions when playing around with assets and files try { var inst = ScriptableObject.CreateInstance(scriptType); AssetDatabase.CreateAsset(inst, path); } catch (Exception e) { Debug.LogException(e); } } } }