• Unity中AssetBundle的变体(Varient)使用教程


    前言

    在使用Unity的AssetBundle过程中,选中资源物体之后,发现右下角的AssetBundle面板,除了有一个命名标签外,还有一个Varient标签,这边文章主要介绍下这个标签的用法,如下图:
    在这里插入图片描述

    使用举例:

    • 多语言:在做游戏开发的过程中,会遇到多语言问题,特别是现在推行的游戏全球化,所以支持多语言是必须要做的。
    • 适配机型:对于不同档次的移动设备,需要使用不同的资源,比如模型的面数、shader的简易程度等,需要用到不同的资源。

    这里我用的Unity版本是2019.4.4,使用了AB包管理插件AssetBundle Browser ,这个可以直接在Windows ->PackageManager里面找到此插件并直接安装,这里以多语言为例,做一个简单的示范。
    首先在Asset目录下新建一个文件夹放置AB包的资源,这个文件夹只要不命名为Resources就行(Resouces是Unity默认的资源防止路径,打包的时候会全部一起打包,使用其中的资源时,会一起加载进内存,导致内存增大);如下图所示:
    在这里插入图片描述
    这两个示例文件夹下面放置的是对应中文和英文UI prefab的资源;接着选中对应的资源,在Inspector面板的AssetBundle位置处,设置Assetbundle Name 和Assetbundle Varient,下图分别是设置中文和英文同样UI的示意图:

    中文UI资源的设置

    在这里插入图片描述

    英文UI资源的设置

    在这里插入图片描述
    之后,选中菜单栏的Windows->AssetBundle Brower,打开设置界面,选中Build栏:
    在这里插入图片描述
    打包成功之后,会在StreamingAssets目录生成对应的AB资源:
    在这里插入图片描述
    然后我们可以在脚本中验证下加载的效果,简单说,就是实现多语言的切换

    示例代码

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ABLoadTest : MonoBehaviour
    {
        private Dictionary<string, AssetBundle> abCache;
        private AssetBundle mainAB = null; //主包
        private AssetBundleManifest mainManifest = null; //主包中配置文件---用以获取依赖包
        private string language;
        GameObject cur_panel;
        AssetBundle ab;
    
    
        private string basePath
        {
            get
            {
    #if UNITY_EDITOR || UNITY_STANDALONE
                return Application.dataPath + "/StreamingAssets/";
    #elif UNITY_IPHONE
                    return Application.dataPath + "/Raw/";
    #elif UNITY_ANDROID
                    return Application.dataPath + "!/assets/";
    #endif
            }
        }
    
        string curabName
        {
            get { return "ui1." + language; }
        }
    
        void Start()
        {
            language = "en";
            abCache = new Dictionary<string, AssetBundle>();
    
            ab = LoadABTest();
            GameObject model = ab.LoadAsset<GameObject>("Canvas");
            cur_panel = Instantiate<GameObject>(model);
            // dosomething
    
    
        }
    
        AssetBundle LoadABTest()
        {
            string abName = curabName;
            if (mainAB == null)
            {
                mainAB = AssetBundle.LoadFromFile(basePath + "StandaloneWindows");
                mainManifest = mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    
            }
            //根据manifest获取所有依赖包的名称 固定API
            string[] dependencies = mainManifest.GetAllDependencies(abName);
            //循环加载所有依赖包
            for (int i = 0; i < dependencies.Length; i++)
            {
                //如果不在缓存则加入
                if (!abCache.ContainsKey(dependencies[i]))
                {
                    //根据依赖包名称进行加载
                    ab = AssetBundle.LoadFromFile(basePath + dependencies[i]);
                    //注意添加进缓存 防止重复加载AB包
                    abCache.Add(dependencies[i], ab);
                }
            }
            //加载目标包 -- 同理注意缓存问题
            if (abCache.ContainsKey(abName))
            {
                Debug.Log($"have load {abName}");
                return abCache[abName];
            }
            else
            {
                ab = AssetBundle.LoadFromFile(basePath + abName);
                abCache.Add(abName, ab);
                Debug.Log($"new load {abName}");
                return ab;
            }
        }
    
        // Update is called once per frame
        void Update()
        {
            if(Input.GetKeyDown(KeyCode.A)) // 同步加载
            {
                if(language != "zh")
                {
                    if (cur_panel != null)
                    {
                        cur_panel.SetActive(false);
                        Destroy(cur_panel);
                    }
                    UnLoad(curabName);
                    language = "zh";
                    ab = LoadABTest();
                    GameObject model = ab.LoadAsset<GameObject>("Canvas");
                    cur_panel = Instantiate<GameObject>(model);
                }
    
            }
                
            else if (Input.GetKeyDown(KeyCode.S))// 异步加载
            {
                if (language != "en")
                {
                    if (cur_panel != null)
                    {
                        cur_panel.SetActive(false);
                        Destroy(cur_panel);
                    }
                    UnLoad(curabName);
                    language = "en";
                    ab = LoadABTest();
                    GameObject model = ab.LoadAsset<GameObject>("Canvas");
                    cur_panel = Instantiate<GameObject>(model);
                }
    
            }
            else if (Input.GetKeyDown(KeyCode.D))// 单个卸载
            {
                string abName = "testuipanel0." + language;
                UnLoad(abName);
                Debug.Log("have UnLoadAll 3dmodel.first");
    
            }
            else if (Input.GetKeyDown(KeyCode.F))// 全部卸载
            {
                UnLoadAll();
                Debug.Log("have UnLoadAll");
            }
    
        }
    
        private IEnumerator LoadResAsyncTest(AssetBundle ab)
        {
            if (ab == null) yield return null;
            string abName = "testuipanel0." + language;
            var model1 = ab.LoadAssetAsync<GameObject>(abName);
            yield return model1;
            var cur_panel = Instantiate((GameObject)model1.asset);
            // dosomething
        }
    
        //====================AB包的两种卸载方式=================
        //单个包卸载
        public void UnLoad(string abName)
        {
            if (abCache.ContainsKey(abName))
            {
                abCache[abName].Unload(false);
                //注意缓存需一并移除
                abCache.Remove(abName);
            }
        }
    
        //所有包卸载
        public void UnLoadAll()
        {
            AssetBundle.UnloadAllAssetBundles(false);
            //注意清空缓存
            abCache.Clear();
            mainAB = null;
            mainManifest = null;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171

    通过按键实现多语言界面的切换


    在这里插入图片描述
    关于AB包的基础使用文章可以看这篇

  • 相关阅读:
    5G边缘计算网关的功能及作用
    MATLB|可视化学习(plot和bar)
    C语言入门(八)一维数组
    net-java-php-python-社区书店管理信息计算机毕业设计程序
    linux上VirtualBox使用
    Vim下更新coc-nvim
    Blazor和Vue对比学习(基础1.2):模板语法和Razor语法
    Postgresql与执行计划相关的配置项
    Linux系统Docker部署Nexus Maven并实现远程访问本地管理界面
    解决 Tomcat 跨域问题 - Tomcat 配置静态文件和 Java Web 服务(Spring MVC Springboot)同时允许跨域
  • 原文地址:https://blog.csdn.net/qq_41841073/article/details/127722505