Unity3D:AssetBundle应用

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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值