question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Useage Question : Awaiting AsyncOperationHandle<SceneInstance>

See original GitHub issue

Hi,

I have created a static helper class with a function that uses Addressable to load a scene and return the AsyncOperationHandle<SceneInstance> for later use. The AsyncOperationHandle<SceneInstance> is is not intended to be awaited, it is just used to call Addressables.UnloadSceneAsync() later on.

I am having an issue awaiting the AsyncOperationHandle<SceneInstance> where i recive this message:

“yield SceneInstance is not supported on await IEnumerator or IEnumerator.ToUniTask(), please use ToUniTask(MonoBehaviour coroutineRunner) instead.”

I don’t have a MonoBehaviour instance to use with ToUniTask(MonoBehaviour coroutineRunner) or actually understand why it is necessary.

Any help on how best to structure this would be really appreciated, thank you!

    public static async UniTask<AsyncOperationHandle<SceneInstance>> LoadSceneAsync(object key, LoadSceneMode loadMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
    {
        var asyncOphandle = Addressables.LoadSceneAsync(key, loadMode, activateOnLoad, priority);
        
        // These commented out lines cause: "yield SceneInstance is not supported on await IEnumerator or IEnumerator.ToUniTask(), please use ToUniTask(MonoBehaviour coroutineRunner) instead."
        //await asyncOphandle;
        //await asyncOphandle.ToUniTask();

        // I can't do this as I don't have a MonoBehaviour to pass in
        //await asyncOphandle.ToUniTask(this);
        
        // This works, but is very inefficient as its using Unity's implementation
        // await asyncOphandle.Task;
        
        // This is what I have settled on for now, is there a better way?
        while (!asyncOphandle.IsDone)
            await UniTask.Yield();
        
        return asyncOphandle;
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
neuecccommented, Jan 26, 2021

Indeed, AsyncOperationHandle<TObject> : IEnumerator may execute its extension method. But if UniTask.Addressables is enabled, AddressableAsyncExtensions has priority. Maybe this method is defined in another asmdef and there is no reference to UniTask.Addressables? Add UniTask.Addressables as a reference.

0reactions
neuecccommented, Jan 27, 2021

If code is grayed out, yes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Struct AsyncOperationHandle | Addressables | 1.6.2
Struct AsyncOperationHandle. Non typed operation handle. This allows for reference counting and checking for valid references.
Read more >
How to Catch Addressables AsyncOperation download errors
Hello, I mark whole scene as addressable and I use this code to download and load scene from server. Scene is about 30mb....
Read more >
Async operation handling | Addressables | 1.9.2
When the operation completes, the AsyncOperationHandle.Status property is either AsyncOperationStatus.Succeeded or AsyncOperationStatus.Failed . If successful, ...
Read more >
Preload multiple scenes at the same time and activate ...
[Unity, async, simultaneous] Preload scene in unity - this question tells us about how we can pre-load a scene in Unity and load...
Read more >
Unity. Can't activate scene instance when it cached after ...
You want to split that and only use e.g. only Addressables.LoadAssetAsync for your caching. And then only load and unload this scene via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found