Create a template
[System.Serializable]//类可以序列化
public class LevelSettings : ScriptableObject
{
public float TotalTime = 60;
public float Gravity = -30;
public AudioClip Bgm ;
public Sprite Background;
}
Declare a method to create an instance in the utility class
/// <summary>
/// 创建ScriptableObject类型的实例子
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">保存路径</param>
/// <returns></returns>
public static T CreateAsset<T>(string path) where T : ScriptableObject
{
//创建内存中T类型实例
T dataClass = ScriptableObject.CreateInstance<T>();
//根据上方的实例创建资源
AssetDatabase.CreateAsset(dataClass, path);
//刷新project
AssetDatabase.Refresh();
//保存
AssetDatabase.SaveAssets();
return dataClass;
}
Add the corresponding button in the Editor menu bar
[MenuItem("Tools/Level Creator/New Level Settings")]
private static void NewLevelSettings()
{
//弹出保存文件对话框
//参数 title,资源默认名称 扩展名 窗体显示主要内容
//返回 string 存储目录
string path = EditorUtility.SaveFilePanelInProject("New Level Settings", "LevelSettings", "asset", "Define the name for the LevelSettings asset.");
//判断路径是否为空
if (!string.IsNullOrEmpty(path))
{
//创建实例
EditorUtils.CreateAsset<LevelSettings>(path);
}
}