Skip to content

Commit 5f8d934

Browse files
author
ChrisSu
committed
Create assets, editor script
1 parent 62626fe commit 5f8d934

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System;
4+
public class CreateAssets
5+
{
6+
[MenuItem("Assets/Create assets")]
7+
public static void CreateAsset()
8+
{
9+
ScriptableObjectUtility.CreateAsset<AnimatorParameter>();
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System.IO;
4+
5+
public static class ScriptableObjectUtility
6+
{
7+
public static void CreateAsset<T>() where T : ScriptableObject
8+
{
9+
T asset = ScriptableObject.CreateInstance<T>();
10+
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
11+
if (string.IsNullOrEmpty(path))
12+
{
13+
Debug.LogError("Not select files, select files first! ");
14+
return;
15+
}
16+
else if (!string.IsNullOrEmpty(Path.GetExtension(path)))
17+
{
18+
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
19+
}
20+
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New" + typeof(T).ToString() + ".asset");
21+
AssetDatabase.CreateAsset(asset, assetPathAndName);
22+
AssetDatabase.SaveAssets();
23+
AssetDatabase.Refresh();
24+
EditorUtility.FocusProjectWindow();
25+
Selection.activeObject = asset;
26+
}
27+
}

0 commit comments

Comments
 (0)