游戏热更新——AssetBundle

AssetBundle

AssetBundle的定义与使用

  AssetBundle是一个压缩包包含模型、贴图、预制体、声音、甚至是整个场景,可以在游戏运行的时候被加载

  AssetBundle自身保存着相互的依赖关系

  压缩包可以使用LZMA和LZ4压缩算法,减少包大小,更快的进行网络传输

  把一些可以下载内容AssetBundle里面,可以减少安装包的大小

什么是AssetBundle

  是一个存在硬盘上的文件,可以称之为压缩包,这个压缩包可以认为是一个文件夹,里面包含了多个文件。这些文件分为两类:serialized file 和 resource files(序列化文件和源文件)

    serialized file:资源被打碎放在一个对象中,最后统一被写进一个单独的文件(只有一个)

    resource file:某些二进制资源(图片、声音)被单独保存,方便快速加载

  是一个AssetBundle对象,可以通过代码从一个特定的压缩包加载出来的对象。这个对象包含了所有当初添加到这个压缩包里面的内容,可以通过这个对象加载出来使用

AssetBundle使用流程图

在这里插入图片描述

在这里插入图片描述

AssetBundle使用流程(AB)

  指定资源的AssetBundle属性(xxxa/xxx)这里xxxa会生成目录,名字为xxx

  构建AssetBundle包

  上传AB包

  加载AB包和包里面的资源

利用代码打包示例

using System.IO;
using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string dir = "AssetBundles";
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

利用代码加载本地AB包示例

using UnityEngine;

public class LoadFromFileExp : MonoBehaviour 
{
    private void Start()
    {
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/wall.unity3d");

        GameObject obj = ab.LoadAsset<GameObject>("wall");
    
        Instantiate(obj);
    }
}

AssetBundle 分组策略(仅供参考)

  逻辑实体分组

    一个UI界面或者所有UI界面一个包(这个界面里面的贴图和布局信息一个包)

    一个角色或者所有角色一个包(这个角色里面的模型和动画一个包)

    所有的场景所共享的部分一个包(包括贴图和模型)

  按照类型分组

    所有声音资源打成一个包,所有Shader打成一个包,所有模型打成一个包,所有材质打成一个包

  按照使用分组

    把在某一时间内使用的所有资源打成一个包。可以按照关卡分,一个关卡所需要的所有资源包括角色、贴图、声音等打成一个包。也可以按照场景分,一个场景所需要的资源一个包

AssetBundle 分组策略——总结

  把经常更新的资源放在一个单独的包里面,跟不经常更新的包分离

  把需要同时加载的资源放在一个包里面

  可以把其他包共享的资源放在一个单独的包里面

  把一些需要同时加载的小资源打包成一个包

  如果对于同一个资源有两个版本,可以考虑通过后缀来区分:比如 unity3dv1unity3dv2

依赖打包

在这里插入图片描述

打包选项(AssetBundle压缩方式)

  Build的路径(随意只要是在硬盘上都可以的)

  BuildAssetBundleOptions

    BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载的时间更长。使用之前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。在下载的时候可以使用LZMA算法,一旦他被下载了之后,它会使用LZ4算法保存到本地上

    BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快

    BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是可以加载指定资源而不用解压全部

    使用LZ4压缩,可以获得可以跟不压缩相媲美的加载速度,而且比不压缩文件更小

  BuildTarget

    选择build出来的AB包要使用的平台

在这里插入图片描述

从内存中加载 AssetBundle

IEnumerator Start()
    {
        AssetBundleCreateRequest ab = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/wall1.unity3d"));

        yield return ab;

        AssetBundle asb = ab.assetBundle;

        GameObject obj = asb.LoadAsset<GameObject>("wall1");

        Instantiate(obj);
    }

从本地加载 AssetBundle

IEnumerator Start()
    {
        AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync("AssetBundles/wall1.unity3d");

        yield return ab;

        AssetBundle asb = ab.assetBundle;

        GameObject obj = asb.LoadAsset<GameObject>("wall1");

        Instantiate(obj);
    }

使用 WWW.LoadFromCacheOrDownload 下载并加载 AssetBundle

IEnumerator Start()
    {
        while (!Caching.ready)
        {
            yield return null;
        }
        WWW www = WWW.LoadFromCacheOrDownload(@"E:\Work\Work2\AssetBundleTest\\AssetBundles\wall1.unity3d", 1);
        yield return www;

        if(!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
            yield break;
        }

        AssetBundle asb = www.assetBundle;

        GameObject obj = asb.LoadAsset<GameObject>("wall1");

        Instantiate(obj);
    }

从服务器端下载 AssetBundle

IEnumerator Start()
    {
        while (!Caching.ready)
        {
            yield return null;
        }
        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/wall1.unity3d", 1);
        yield return www;

        if(!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
            yield break;
        }

        AssetBundle asb = www.assetBundle;

        GameObject obj = asb.LoadAsset<GameObject>("wall1");

        Instantiate(obj);
    }

使用 UnityWebRequest 来加载 AssetBundle

IEnumerator Start()
    {
        string uri = @"file:///E:\Work\Work2\AssetBundleTest\\AssetBundles\wall1.unity3d";
        UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(uri);

        yield return uwr.SendWebRequest();

        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(uwr);


        GameObject obj = ab.LoadAsset<GameObject>("wall1");

        Instantiate(obj);
    }

加载 Manifest

IEnumerator Start()
    {
        string uri = @"file:///E:\Work\Work2\AssetBundleTest\\AssetBundles\wall1.unity3d";
        UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(uri);

        yield return uwr.SendWebRequest();

        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(uwr);

        GameObject obj = ab.LoadAsset<GameObject>("wall1");

        Instantiate(obj);

        AssetBundle manifestAb = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAb.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        foreach(var a in manifest.GetAllAssetBundles())
        {
            print(a);
        }

        string[] strs = manifest.GetAllDependencies("wall1.unity3d");
        foreach(var a in strs)
        {
            print(a);
            AssetBundle.LoadFromFile(@"AssetBundles/" + a);
        }
    }

AssetBundle 的卸载

  卸载有两个方面:

    减少内存使用

    有可能导致丢失

  AssetBundle.Unload(true):卸载所有资源,即使有资源被使用着

    1.在关卡切换、场景切换

    2.资源没被用的时候调用

  AssetBundle.Unload(false):卸载所有没有被使用的资源

    个别资源的卸载:

      1.通过Resource.UnloadUnusedAssets

      2.场景切换的时候自动卸载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值