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