Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created November 23, 2019 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiarheiPilat/8362f3df2cfcc19d66d50f3a27c80151 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/8362f3df2cfcc19d66d50f3a27c80151 to your computer and use it in GitHub Desktop.
Create a ScriptableObject from script.
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