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的构建平台。

 

posted @ 2019-03-11 13:49  田错  阅读(882)  评论(0编辑  收藏  举报