rpm-unity-sdk-core icon indicating copy to clipboard operation
rpm-unity-sdk-core copied to clipboard

Timing issues on LoadAvatarAsync vs OnCompleted

Open ripperdoc opened this issue 9 months ago • 3 comments

Describe the bug When I load multiple avatars, sometimes LoadAvatarAsync finishes before OnCompleted event is called. This results in racing conditions where the avatar sometimes isn't ready to use even if all async loading tasks seem completed.

Files

This is not specific to any specific avatar URL. Cannot share project code at this point.

To Reproduce

Roughly this code:

    private void Start()
    {
        rpmLoader = new AvatarObjectLoader();
        rpmLoader.OnCompleted += StorePreloadedAvatar;
    }

    private void StorePreloadedAvatar(object sender, CompletionEventArgs completionEventArgs)
    {
        Debug.Log($"{completionEventArgs.Url} OnCompleted");
    }

    public async Task<LoadResult> LoadRpmAvatar(string url)
    {
        Debug.Log($"{url} before LoadAvatarAsync");
        var rpmResponse = await rpmLoader.LoadAvatarAsync(url);
        Debug.Log($"{url} after LoadAvatarAsync");
    }

    public async void StartLoading()
    {
        await Task.WhenAll([
            LoadRpmAvatar("https://models.readyplayer.me/6523a41a7137f30e3a522e4a.glb"),
            LoadRpmAvatar("https://models.readyplayer.me/64ef0b3542c59d7dceb2f468.glb"),
            LoadRpmAvatar("https://models.readyplayer.me/656db9a9165ecd09deacd570.glb")
        ]);
    }

This will sometimes output this log:

https://models.readyplayer.me/6523a41a7137f30e3a522e4a.glb before LoadAvatarAsync
https://models.readyplayer.me/64ef0b3542c59d7dceb2f468.glb before LoadAvatarAsync
https://models.readyplayer.me/656db9a9165ecd09deacd570.glb before LoadAvatarAsync
https://models.readyplayer.me/6523a41a7137f30e3a522e4a.glb OnCompleted
https://models.readyplayer.me/6523a41a7137f30e3a522e4a.glb after LoadAvatarAsync
https://models.readyplayer.me/64ef0b3542c59d7dceb2f468.glb after LoadAvatarAsync
https://models.readyplayer.me/656db9a9165ecd09deacd570.glb after LoadAvatarAsync
https://models.readyplayer.me/64ef0b3542c59d7dceb2f468.glb OnCompleted
https://models.readyplayer.me/656db9a9165ecd09deacd570.glb OnCompleted

Expected behavior

I expect the loading to be in order, so that OnComplete always fires before LoadAvatarAsync finishes. The fix is to create a separate behaviour to ensure all OnComplete events have been called, but that basically means I have no use for LoadAvatarAsync and could just load it will traditional callback / Coroutine style code.

Desktop (please complete the following information):

  • Ready Player Me Core version 6.2.4
  • glTFast version 6.0.1
  • Unity Editor version 2022.3.9f1
  • URP
  • Operating System Mac
  • Platform: Editor

ripperdoc avatar May 08 '24 11:05 ripperdoc