Created
June 9, 2020 20:37
-
-
Save ryanmillerca/0ae7397ee364e9a01ca3b693c50d8024 to your computer and use it in GitHub Desktop.
ScriptableObject Helper
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
#if UNITY_EDITOR | |
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
/// <summary> | |
/// Utility for creating ScriptableObject in Unity | |
/// </summary> | |
public static class CustomAssetUtility | |
{ | |
/// <typeparam name="T"></typeparam> | |
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"); | |
Debug.Log("Created new asset at: " + assetPathAndName); | |
AssetDatabase.CreateAsset(asset, assetPathAndName); | |
AssetDatabase.SaveAssets(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
} | |
/// <typeparam name="T"></typeparam> | |
public static void CreateAsset<T>(string path) where T : ScriptableObject { | |
T asset = ScriptableObject.CreateInstance<T>(); | |
AssetDatabase.CreateAsset(asset, path); | |
AssetDatabase.SaveAssets(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
} | |
/// <typeparam name="T"></typeparam> | |
public static T CreateAndGetAsset<T>(string path) where T : ScriptableObject { | |
T asset = ScriptableObject.CreateInstance<T>(); | |
AssetDatabase.CreateAsset(asset, path); | |
AssetDatabase.SaveAssets(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
return asset; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment