unity编辑器扩展--AssetBundle菜单

选中预制体,点击菜单按钮打包到固定文件夹

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;

public class CreatAssetBundles{
    const string PATH = "Data/asset/"const string fileEndName = ".aseetBundle";
    [MenuItem("MyTools/打包AB/打包选中的对象")]                                   
    static void CreateAssetBundlesMain()
    {
        Object[] selectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        if (selectedAsset.Length == 0)
        {
            Debug.LogError("未选中任何对象!");
            return;
        }

        string toPath = GetAssetBundlePath();

        foreach (var obj in selectedAsset)
        {
            string objName = obj.name;
            //Debug.Log(toPath);

            if (!Directory.Exists(toPath))
            {
                Directory.CreateDirectory(toPath);

            }

            string targetPath = toPath + objName.ToLower() + fileEndName;
            if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, GetTargetPlatform()))
            {
                Debug.Log(objName + "资源打包成功");
            }else
            {
                Debug.Log(objName + "资源打包失败");
            }

        }
    }
    [MenuItem("MyTools/打包AB/按AB包名打包所以对象")]
    static void BuildleAllAssetBundles()
    {
                 BuildPipeline.BuildAssetBundles("Assets/Data",BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, GetTargetPlatform());
    }

    public static string GetAssetBundlePath()
    {
        BuildTarget target = GetTargetPlatform();
        return Application.dataPath + PATH + target.ToString().ToLower() + "/";
    }

    public static BuildTarget GetTargetPlatform()
    {
        BuildTarget target = BuildTarget.Android;
#if UNITY_STANDALONE
        target = BuildTarget.StandaloneWindows;
#elif UNITY_IPHONE
         target = BuildTarget.iOS;
#elif UNITY_ANDROID
        target = BuildTarget.Android;
#endif

        return target;
    }
}

猜你喜欢

转载自blog.csdn.net/kill566666/article/details/78406210