Awaiting Addressable.LoadAssetAsync and Yield hangs in Editor
Hello. I am experiencing an odd behavior in the editor (2020.3.48f1) with the latest 2.5.4, where the statement await Addressables.LoadAssetAsync<TextAsset>(key) hangs when ran in edit mode.
By "hangs", I mean that the statement does not return unless I manually trigger an editor update by refreshing the scene or interacting with editor UI (e.g. resizing the game view).
In trying to reproduce this issue, I have isolated an interesting issue that sounds possibly related to my problem. Try to run the following code in the editor:
[InitializeOnLoadMethod]
private static void OnEditorStarts()
{
UniTask.Create(async () =>
{
await Addressables.InitializeAsync();
Debug.LogError("!init");
for (int i = 0; i < 10; i++)
{
var a = Addressables.LoadAssetAsync<TextAsset>("some_addressable_asset.txt");
try
{
await a;
}
finally
{
Addressables.Release(a);
}
Debug.LogWarning(i);
await UniTask.Yield();
}
Debug.LogError("!asset");
})
.Forget();
}
You should observe two log lines: "!init" and "1". The code is hanging on await UniTask.Yield().
Now, if you interact with Unity, the task continues and you will see "2", "3", etc as long as you keep interacting with Unity. If you don't move, nothing happens.
Commenting await UniTask.Yield(), on the other hand, makes this work correctly.
It gets even weirder. If you add another await UniTask.Yield() before the for loop, this one continues correctly. Same if you comment await a instead of await UniTask.Yield(). So the issue seems to be not specifically in await UniTask.Yield() or await a in the combination of the two. How can it be?
Seems pretty strange. It might have to do with the fact that you're using a lambda (but to be honest, I don't know what UniTask.Create() does. It could also have something to do with running addressables in the editor instead of play mode.
Try changing the code to something like this (note that the return is explicitly a UniTask type):
private static void OnEditorStarts()
{
OnEditorStartsAsync().Forget();
}
private static async UniTaskVoid OnEditorStartsAsync()
{
//.... your code here
}