AssetBundle是什么:
AssetBundle文件,也叫AB包,可以存储任何一种Unity可以识别的资源,如模型、纹理图、音频、场景等资源,也可加载开发者自定义的二进制文件。AssetBundle支持3种格式的压缩,分别是LZMA、LZ4、无压缩,默认是LZMA格式的压缩,使资源文件体积减小,便于下载和传播。
一:打包AssetBundle
使用到的API
打包AssetBundle
BuildPipeline.BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
案例代码
using System.IO;
using UnityEditor;
using UnityEngine;
public class AssetBundleEditor
{
private static string ASSETBUNDLEDIRECTORY = Application.streamingAssetsPath + "/" + "AssetBundles/";
[MenuItem("工具/生成AssetBundle")]
public static void GeneratorAssetBundle()
{
if (!Directory.Exists(ASSETBUNDLEDIRECTORY))
{
Directory.CreateDirectory(ASSETBUNDLEDIRECTORY);
}
BuildPipeline.BuildAssetBundles(ASSETBUNDLEDIRECTORY, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
使用教程
设置Prefab的AssetBundle属性
点击脚本中定义的选项卡,生成AssetBundle目录
二:加载AssetBundle
使用到的API
同步加载AssetBundle
AssetBundle.LoadFromFile(path)
异步加载AssetBundle
AssetBundle.LoadFromFileAsync(path)
同步加载AssetBundle案例代码
using UnityEngine;
public class Test : MonoBehaviour
{
private const string ASSETBUNDLEDIRECTORY = "AssetBundles";//AssetBundle目录名
// Start is called before the first frame update
void Start()
{
string directoryPath = Application.streamingAssetsPath + "/" + ASSETBUNDLEDIRECTORY + "/";
string assetBundleName = "test";//打包时Prefab设置的AssetBundle属性
AssetBundle assetBundle = AssetBundle.LoadFromFile(directoryPath + assetBundleName);
GameObject cubePrefab = assetBundle.LoadAsset("Cube") as GameObject;//该AssetBundle中的资源名称
GameObject.Instantiate(cubePrefab);
}
}
异步加载AssetBundle案例代码
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour
{
private const string ASSETBUNDLEDIRECTORY = "AssetBundles";//AssetBundle目录名
// Start is called before the first frame update
void Start()
{
StartCoroutine(InstantiateAssetBundle());
}
IEnumerator InstantiateAssetBundle()
{
string directoryPath = Application.streamingAssetsPath + "/" + ASSETBUNDLEDIRECTORY + "/";
string assetBundleName = "test";//打包时Prefab设置的AssetBundle属性
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(directoryPath + assetBundleName);
yield return request;
AssetBundle assetBundle = request.assetBundle;
GameObject cubePrefab = assetBundle.LoadAsset("Cube") as GameObject;//该AssetBundle中的资源名称
GameObject.Instantiate(cubePrefab);
}
}
三:卸载AssetBundle
注意:卸载是指卸载内存中的数据,场景中的物体并不会被销毁
使用到的API
AssetBundle.Unload(bool)
仅卸载AssetBundle,不卸载已实例化的对象
AssetBundle assetbundle;//通过加载API加载出来的AssetBundle
assetbundle.Unload(false);
卸载AssetBundle,并且卸载通过该AssetBundle实例化的对象
AssetBundle assetbundle;//通过加载API加载出来的AssetBundle
assetbundle.Unload(true);
知识扩展:
压缩格式的优缺点:
LZMA:高压缩、低性能、高内存、体积最小格式。
LZ4 :中压缩、高性能、低内存、性价比最高格式。
默认打包压缩的格式为LZMA
BuildPipeline.BuildAssetBundles(ASSETBUNDLEDIRECTORY, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
修改压缩格式为LZ4
BuildPipeline.BuildAssetBundles(ASSETBUNDLEDIRECTORY, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);
当然,我们也可以不对AssetBundle进行压缩。没有经过压缩的包体积最大,但是访问速度最快。
BuildPipeline.BuildAssetBundles(ASSETBUNDLEDIRECTORY, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows);