Skip to content

Instantly share code, notes, and snippets.

@TarasOsiris
Last active August 29, 2015 13:56
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 TarasOsiris/9252536 to your computer and use it in GitHub Desktop.
Save TarasOsiris/9252536 to your computer and use it in GitHub Desktop.
Generic method to create .asset files. Taken from here http://www.jacobpennock.com/Blog/?page_id=715 and modified a bit.
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// Taken from here : http://www.jacobpennock.com/Blog/?page_id=715 and modified
/// </summary>
public static class CustomAssetUtility
{
public static void CreateAsset<T>() where T : ScriptableObject
{
T asset = ScriptableObject.CreateInstance<T>();
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (path == string.Empty)
{
path = "Assets";
}
else if (Path.GetExtension(path) != string.Empty)
{
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), string.Empty);
}
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T) + ".asset");
AssetDatabase.CreateAsset(asset, assetPathAndName);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment