同时Instance大量AssetBundle中的gameobjects,容易产生卡顿,影响用户游戏体验。对此我们提出了异步Instantiate,用户可以使用InstantiateAsync API 完成异步Instantiate工作,从而避免卡顿。使用InstantiateAsync API不会影响原始AssetBundle的工作流程,比如当你想场景中创建游戏对象,只需在AssetBundle加载完成之后,异步调用 InstantiateAsync API,不影响Assetbundle其余流程。
public class Example : MonoBehaviour
{
AssetBundle myLoadedAssetBundle;
void Start()
{
//load Assetbundle
myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
}
void Update()
{
// Press the space key to start coroutine
if (Input.GetKeyDown(KeyCode.Space))
{
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
return;
}
// Use a coroutine to Instantiate in the background
StartCoroutine(InstantiateAsyc());
}
}
IEnumerator InstantiateAsyc()
{
yield return myLoadedAssetBundle.InstantiateAsync(myLoadedAssetBundle.GetAllAssetNames()[0]);
}
}