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

Unity中AssetBundle的变体(Varient)使用教程在使用 Unity 的 AssetBundle 过程中 选中资源物体之后 发现右下角的 AssetBundle 面板 除了有一个命名标签外 还有一个 Varient 标签 这边文章主要介绍下这个标签的用法 unityvariant

大家好,欢迎来到IT知识分享网。

前言

在使用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; } } 

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


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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/141000.html

(0)
上一篇 2025-05-22 21:15
下一篇 2025-05-22 21:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信