Created
November 23, 2019 22:20
-
-
Save SiarheiPilat/8362f3df2cfcc19d66d50f3a27c80151 to your computer and use it in GitHub Desktop.
Create a ScriptableObject from script.
This file contains 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 UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
public static class ScriptableObjectUtility | |
{ | |
/// <summary> | |
/// Create, name and place unique new ScriptableObject asset files. | |
/// Taken from: https://wiki.unity3d.com/index.php/CreateScriptableObjectAsset | |
/// </summary> | |
// Use example: | |
// | |
// public class YourClassAsset | |
// { | |
// [MenuItem("Assets/Create/YourClass")] | |
// public static void CreateAsset() | |
// { | |
// ScriptableObjectUtility.CreateAsset<YourClass>(); | |
// } | |
// } | |
public static void CreateAsset<T>() where T : ScriptableObject | |
{ | |
T asset = ScriptableObject.CreateInstance<T>(); | |
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 " + typeof(T).ToString() + ".asset"); | |
AssetDatabase.CreateAsset(asset, assetPathAndName); | |
AssetDatabase.SaveAssets(); | |
AssetDatabase.Refresh(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment