[Note that I’m unable to edit subject. The affected version is 2021.2.7. I assume this will be fixed before LTS
]
As far as I can see, WaitForCompletion() is completely broken and will hang the editor (or any built player) in Unity 2021.2.7 / Addressables 1.19.15. The same code works as expected in our live 2020.3 project.
A bug report will follow as soon as I’ve made a custom project. But setting up a new project and configuring/building/uploading to addressable servers takes time, and this issue appears trivially reproducible with no custom setup so reporting here first for efficiency.
Instructions:
- Create a new empty scene.
- Create a prefab (a basic cube is sufficient reproduce) and make addressable.
- Create a new gameobject in the empty scene and attach the code below. Point assetReferenceGameObject at the cube prefab (or whatever) you just made.
- Run project in “Use Asset Database”, observe works correctly.
- Build data, upload to your web server, run using “Use Built Data”, observe editor hangs on entering playmode.
- If required, confirm that commenting in the yield line instead of WaitForCompletion (i.e. making things async) will work
- Note that same code works as expected in 2020.3.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
public class HangOnLoadAddressable : MonoBehaviour
{
public AssetReferenceGameObject assetReferenceGameObject;
public GameObject instantiatedGameObject;
// Start is called before the first frame update
IEnumerator Start()
{
if (!instantiatedGameObject)
{
var asyncOpHandle = assetReferenceGameObject.InstantiateAsync(this.transform);
//yield return asyncOpHandle;
asyncOpHandle.WaitForCompletion();
instantiatedGameObject = asyncOpHandle.Result;
}
yield break; // Here just to allow compilation
}
}