-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptableObjectUtility.cs
37 lines (30 loc) · 1.09 KB
/
ScriptableObjectUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// Creates a ScriptableObject based on the T parameter
/// How to use: Simple place it into your Unity project and call CreateAsset when required.
/// </summary>
public static class ScriptableObjectUtility
{
public static ScriptableObject 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;
return asset;
}
}