Unity3d AssetBundle 资源加载与管理

设置AssetBundle

BuildPipeline.BuildAssetBundles(
    outPath, 
    builds.ToArray(), 
    BuildAssetBundleOptions.ChunkBasedCompression, 
    BuildTarget.StandaloneWindows64
    );

 

outPath是AssetBundle输出的目录,我们设置为StreamingAssets目录。

包体内的AssetBundle只能放在StreamingAsstes目录,别的目录是无法读取的。

加载AssetBundle之前,需要使用AssetBundleManifest提取每个AssetBundle的相互依赖关系。

string outPath = Path.Combine(Application.dataPath, "StreamingAssets");
//如果目录已经存在删除它
if (Directory.Exists(outPath)){
    Directory.Delete(outPath, true);
}
Directory.CreateDirectory(outPath);

 

builds是AssetBundleBuild类型的泛型集合

List<AssetBundleBuild> builds = new List<AssetBundleBuild>();

每个AssetBundle都需要一个AssetBundleBuild类型的实例对象来设置需要打包的文件和打包后的文件名

builds.Add(new AssetBundleBuild() {
    assetBundleName = "Cube.unity3d",
    assetNames = new string[] {
        "Assets/Cube.prefab",
        "Assets/Cube 2.prefab"
    }
});
builds.Add(new AssetBundleBuild() {
    assetBundleName = "NewMaterial.unity3d",
    assetNames = new string[] {
        "Assets/New Material.mat"
    }
});

 

BuildAssetBundleOptions.ChunkBasedCompression是AssetBundle提供的一种压缩格式

1.LZMA压缩,压缩体积小,但是每次使用都要解压,可能会卡顿,不建议使用。

2.BuildAssetBundleOptions.UncompressedAssetBundle 不压缩,缺点是构建出来的AssetBundle体积比较大,有点事加载比较快。

但可以将不被压缩的构建出来后再用第三方压缩算法压缩他上传到CDN,这样下载时还是压缩过的但使用时是不压缩的读取也更快了。

3.BuildAssetBundleOptions.ChunkBasedCompression LZ4压缩,他是介于二者之间的折中方案,构建后的体积会比不压缩的小一些,加载速度比LZMA快一些,建议使用。

 

BuildTarget.StandaloneWindows64是Bundle文件的构建平台。

每个平台下的Bundle都不一样,需要指定BuildTarget的构建平台。

 

转载于:https://www.cnblogs.com/errornull/p/10455565.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值