Unity生成.assets

工具类

using UnityEngine;
using UnityEditor;
using System.IO;

public static class ScriptableObjectUtility
{
    public static void CreateAsset<T>() where T : ScriptableObject
    {
        T asset = ScriptableObject.CreateInstance<T>();
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if( string.IsNullOrEmpty( path ) )
        {
            Debug.LogError("Not select files, select files first! ");
            return;
        }
        else if (!string.IsNullOrEmpty(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;
    }
}
菜单按钮
using UnityEngine;
using UnityEditor;
using System;
public class CreateAssets
{
    [MenuItem("Assets/Create assets")]
    public static void CreateAsset()
    {
        ScriptableObjectUtility.CreateAsset<AssetsScripts>();
    }
}
被生成对象,ScriptableObject类型

using UnityEngine;
using UnityEditor;
using System;
public class AssetsScripts : ScriptableObject
{
    public string playerName = "Yu Xikuo";
}


发布了469 篇原创文章 · 获赞 113 · 访问量 159万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览