Unity AssetBundle -- AssetBundle打包

关于AssetBundle的打包,Unity提供了新的打包方法BuildPipeline.BuildAssetBundles()和它的重载,之前的BuildPipeline.BuildAssetBundle()已经弃用了

一、BuildPipeline.BuildAssetBundles(path, option, platform)
该方法可以将需要打包的资源单独打成assetbundle文件和对应的manifest文件,但前提是该资源需要进行手动设置assetbundle的名称

	[MenuItem("AssetBundle/Build AssetBundle (Single)")]
    static void BuildAssetBundleSingle()
    {
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/StreamingAssetsSingle", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();
    }

创建一个下拉菜单,名为"AssetBundle/Build AssetBundle (Single)",然后使用BuildPipeline.BuildAssetBundles(path, option, platform)。path是打包到的路径,option是个枚举,表示操作类型,platform同样是枚举,表示打包成哪个平台的文件。

之后将需要打包的文件手动设置assetbundle名称后,就可以进行打包
在这里插入图片描述

二、BuildPipeline.BuildAssetBundles(path, builds, option, platform)
在这里插入图片描述

第二种打包方式是第一种扩展,该方法会将参数builds下提供的路径下的资源全部打到一个assetbundle中。
相对与第一个方法,第二个方法额外提供了一个AssetBundleBuild[]类型的结构体builds,一般情况下,只需要提供builds下面的assetBundleName字段值和assetNames字段值即可进行打包。

这里提供一个简单的合并打包的例子

	[MenuItem("AssetBundle/Build AssetBundle(Co-All)")]
	static List<string> allAssets = new List<string>();
    static void BuildAssetBundleAll()
    {
            AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
            buildMap[0].assetBundleName = "AllBundle";

            GetAssets();

            buildMap[0].assetNames = allAssets.ToArray();
            BuildPipeline.BuildAssetBundles(Application.dataPath + "/StreamingAssetsAll", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
            AssetDatabase.Refresh();

    }

    private static void GetAssets()
    {
            GetAssetPath("Materials");
            GetAssetPath("Prefabs");
            GetAssetPath("Textures");
    }

    private static void GetAssetPath(string file)
    {
            string fullPath = Application.dataPath + "/" + file;
            string replaceStr = Application.dataPath.Replace("/", "\\");


            if (Directory.Exists(fullPath))
            {
                    DirectoryInfo direction = new DirectoryInfo(fullPath);
                    FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

                    if (files.Length <= 0)
                    {
                            Debug.LogWarning("files is null! : " + fullPath);
                    }

                    for (int i = 0; i < files.Length; i++)
                    {
                            if (files[i].Name.EndsWith(".meta"))
                            {
                                    continue;
                            }

                            allAssets.Add(files[i].FullName.Replace(replaceStr, "Assets"));
                    }
            }
            else
            {
                    Debug.LogError(fullPath + " is not exist!");
            }
    }

通过GetAssets()方法中提供的文件夹中的资源来获取该资源的路径,需要注意的是files.FullName提供的是绝对路径,builds中的assetNames字段需要的是Assets下的相对路径,需要简单的转换。

将所有获取到的相对路径提供给assetNames字段,最后调用方法进行打包。之后添加新资源或者新文件夹,只需将资源放到对应的目录中,并在GetAssets()方法中添加GetAssetPath(“FileName”)即可。

猜你喜欢

转载自blog.csdn.net/weixin_44158418/article/details/85126077