IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 解决 Unity3D AssetBundle 异步加载与同步加载冲突问题 -> 正文阅读

[游戏开发]解决 Unity3D AssetBundle 异步加载与同步加载冲突问题

当调用AB包异步加载时,如果接下来的流程中需要同步加载相同的AB包,会出现什么情况呢?让我们做下实验。

我首先生成了一个叫img的AB包,里面放了一些图片和模型

接着我们编写了测试代码,看看会怎样输出

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    AssetBundleCreateRequest _abcr = null;

    void Start()
    {

        _abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/img");
        Debug.Log($"Start() isDone={_abcr.isDone} _abcr.assetBundle ={ (_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");

        AssetBundle _assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/img");
        Debug.Log($"Start() isDone={_abcr.isDone} _abcr.assetBundle ={ (_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");
        Debug.Log($"Start() _assetBundle={(_assetBundle == null ? "null" : _assetBundle.name + " " + _assetBundle.GetHashCode())}");
    }

    void Update()
    {
        Debug.Log($"Update() isDone={_abcr.isDone.ToString()} _abcr.assetBundle ={(_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");

    }
}

?我们先异步加载AB包,然后接着同步加载AB包,输出结果如下

?如图所示,异步加载后,再同步加载,直接抛出的异常提示不允许重复加载AB包,并且同步加载返回了null。 但是虽然同步加载返回了null,可调用同步加载后,异步加载还是被立即强制同步完成了。(笔者认为这个可能是个bug ),那么当我们需要处理这种冲突时如何处理呢?

我们这样修改代码,再看输出

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    AssetBundleCreateRequest _abcr = null;


    void Start()
    {



        _abcr = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/img");
        Debug.Log($"Start() isDone={_abcr.isDone} _abcr.assetBundle ={ (_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");

        _abcr.assetBundle.Unload(false);
        Debug.Log($"Start() isDone={_abcr.isDone} _abcr.assetBundle ={ (_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");

        AssetBundle _assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/img");
        Debug.Log($"Start() isDone={_abcr.isDone} _abcr.assetBundle ={ (_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");
        Debug.Log($"Start() _assetBundle={(_assetBundle == null ? "null" : _assetBundle.name + " " + _assetBundle.GetHashCode())}");

    }

    void Update()
    {
        Debug.Log($"Update() isDone={_abcr.isDone.ToString()} _abcr.assetBundle ={(_abcr.assetBundle == null ? "null" : _abcr.assetBundle.name + " " + _abcr.assetBundle.GetHashCode())}");


     }
}

我们这次先异步加载,然后再使用请求中的AssetBundle对象的Unload(true)方法,在未加载完成时让其直接卸载,然后在同步加载。输出如下

可见,我们在异步加载后使用AssetBundleCreateRequest.assetBundle.Unload(true)直接卸载 .?使得异步加载立即完成且assetBundle=null.然后我们再发起同步加载,则成功的获得了AB包对象。但是我们注意到异步加载的AB包对象和同步加载的AB包对象的哈希不同,已经不是同一个对象了。

那么AB包的异步同步冲突我看测试完了,但是加载资源对象的异步同步冲突又会如何呢?

我们使用如下代码再做试验:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{

    AssetBundleRequest _abr = null;

    void Start()
    {
        AssetBundle _ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/img");

        _abr = _ab.LoadAllAssetsAsync();
        Debug.Log($"Start() _abr.isDone={_abr.isDone.ToString()} _abr.allAssets={(_abr.allAssets == null ? "null" : _abr.allAssets.Length.ToString() + " " + _abr.allAssets[0].GetHashCode())}");

        UnityEngine.Object[] _allAssets = _ab.LoadAllAssets();
        Debug.Log($"Start() _abr.isDone={_abr.isDone.ToString()} _abr.allAssets={(_abr.allAssets == null ? "null" : _abr.allAssets.Length.ToString() + " " + _abr.allAssets[0].GetHashCode())} ");
        Debug.Log($"Start() _allAssets={(_allAssets == null ? "null" : _allAssets.Length.ToString() + " " + _allAssets[0].GetHashCode())}");
    }

    void Update()
    {
        Debug.Log($"Update() _abr.isDone={_abr.isDone.ToString()} _abr.allAssets={(_abr.allAssets == null ? "null" : _abr.allAssets.Length.ToString() + " " + _abr.allAssets[0].GetHashCode())} ");
    }
}

我们先加载AB包,然后用AB包对象异步加载所有资源对象,之后我们在用此AB包对象同步加载所有资源对象。输出如下

和AB包的异步同步冲突抛出异常不同,当我们在异步加载后发起同步加载,异步请求立即完毕,且异步和同步都返回了资源对象,而且我们可以看到返回的资源对象哈希值相同是同一个对象。总之,资源加载异步同步之间没有冲突,如果先发起异步后发起同步,则异步会立即完成。?

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-09-15 02:18:44  更:2022-09-15 02:18:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年4日历 -2025/4/21 19:21:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码