- Bundle构建工具
- Budle Build策略
- 按文件夹打包
1)优势:budle数量少,小包模式,首次下载快
2)劣势:后期更新的时候,更新补丁大,即使一个budle中只有一个资源发生了改变,也要全部更新整个budle包 - 按文件打包(推荐)
1)优势:更新补丁小
2)劣势:小包模式:首次下载稍慢
- 按文件夹打包
- 由于打bundle 的功能是编辑器下面的功能,因此要放在Editor文件夹下面进
- Budle Build策略
- Bundle 工具构建的流程
- 首先要明确的是,打包的数量为1-*个, 因此先创建一个存储bundle的数组或者列表
List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
- 将你要打包的所有文件进行搜索并保存起来
string[] files = Directory.GetFiles(PathUtil.BundleResourcesPath,"*", SearchOption.AllDirectories);
需要注意的是,使用这个方法去搜索的时候,会将一些meta文件也搜索出来,因此需要在下一步进行一下过滤 - 文件过滤
- 首先要明确的是,打包的数量为1-*个, 因此先创建一个存储bundle的数组或者列表
for(int i = 0; i < files.Length;i++)
{
//将meta文件进行过滤
if(files[i].EndsWith(".meta"))
{
continue;
}
。。。
}
4. 现在把所有需要打bundle包的文件都搜索出来了,下一步则是需要设置一下(AssetBundleBuild的assetName、assetBundleName两个属性)
//如果不是meta文件的话
//由于添加的时候需要设置AssetBundleBuild的属性,如assetNames(unity中的相对目录)
AssetBundleBuild assetBundleBuild = new AssetBundleBuild();
//获取文件的相对目录
string fileName = PathUtil.getStandardPath(files[i]);
Debug.Log("file :" + fileName);
string assetName = PathUtil.GetunityPath(fileName);
assetBundleBuild.assetNames = new string[] { assetName};//绝对路径目的是找到要打包的文件
string bundleName = files[i].Replace(PathUtil.BundleResourcesPath, "").ToLower();
assetBundleBuild.assetBundleName = bundleName + ".ab";//相对路径,给打包出来的文件名命
Debug.Log("assetbunldName: " + bundleName);
assetBundleBuilds.Add(assetBundleBuild);
5. 判断输出路径是否为空,如果不为空就,删除,然后重建一个文件
if (Directory.Exists(PathUtil.BundleOutPath))
Directory.Delete(PathUtil.BundleOutPath, true);
Directory.CreateDirectory(PathUtil.BundleOutPath);
6. 按照之前的命名设置,以及下面这条语句的输出路径设置,要打包的Assbundlebuild文件,压缩格式的设置,运行平台的设置进行打包
BuildPipeline.BuildAssetBundles(PathUtil.BundleOutPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.None, target);
//按照之前的命名设置,这条语句的输出路径,要打包的文件,压缩格式,运行的平台,进行bundle的打包
- 实用类PathUtil
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathUtil {
//这个类是一个实用类,用来管理各种路径的
//根目录
public static readonly string AssetsPath = Application.dataPath;
//需要打Bundle的目录
public static readonly string BundleResourcesPath = AssetsPath + "/BuildResources/";//获取的是所有要打包的文件
//Bundle 的输出目录
public static readonly string BundleOutPath = Application.streamingAssetsPath;
/// <summary>
/// 获取Unity的相对路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetunityPath(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
return path.Substring(path.IndexOf("Assets"));
}
public static string getStandardPath(string path)
{
if (string.IsNullOrEmpty(path))
return string.Empty;
return path.Trim().Replace("\\", "/");
}
}
实现效果