Search Unity

Cancel Async Load [SOLVED]

Discussion in 'Scripting' started by looytroop, Jul 31, 2019.

  1. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    Hello everyone! I have tried looking into this issue on forum posts, however, all of the forums have no answers, or are really old.

    I just want to know if I can async load a scene, then, partway through loading the scene, cancel the async load request, and load a different scene instead.

    I have tried setting the AsyncOperation variable to a different async loading scene, but it does not seem to have worked.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    No, there's no way to cancel a scene load. You would need to wait for it to finish, then unload it. If you use additive scene loading, you might be able to obscure that transition from the user though.
     
  3. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    @WallaceT_MFM thank you for the info, I will try and switch scenes on scene load, thanks!
     
  4. ihgyug

    ihgyug

    Joined:
    Aug 5, 2017
    Posts:
    194
    Have you tried setting allowSceneActivation property to false? (AsyncOperation.allowSceneActivation). That will prevent the scene from activating, and I guess you can just load another scene, and then set back the property to true.
     
  5. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    I have tried, however, I was unable to get the effect that I desired. I basically just have resorted to letting it load, and on the first frame of the scene that gets loaded, I am loading back to the scene I actually want. It is so fast that the player doesn't even notice.
     
  6. Desertleaf

    Desertleaf

    Joined:
    Dec 15, 2013
    Posts:
    15
    This may work with a small to empty scene, but the changes will be noticeable for high density scenes.
     
  7. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    This may be true, although I can't remember exactly at this point, I believe this is the approach that we ended up going with and it worked for our purposes. We shipped our game on Steam and Xbox One October 2019 and I believe I was just reloading on frame 1.
     
  8. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    If I have to let the scene be loaded, then unload it if I wanted to abort it afterwards. This sounds strange right? If I want to cancel the loading operation of a complex scene, will I see the scene flash?

    I actually had tried to set the activateOnLoad parameter to false. But I encountered a weird bug where the handle was invalid right after executing Addressables.LoadSceneAsync. I am using Addressables 1.16.19 and Unity 2019.4.22f1. My code was just:
    Code (CSharp):
    1. Handle = Addressables.LoadSceneAsync(key, LoadSceneMode.Single, false);
    2. yield return Handle;
    3.  
    4. if (_isAborted)
    5. {
    6.     if (Handle.IsValid())
    7.     {
    8.         Addressables.Release(Handle);
    9.     }
    10.  
    11.     yield break;
    12. }
    13.  
    14. // the handle is invalid, "sometimes", not 100% reproducible.
    15. yield return Handle.Result.ActivateAsync();
    16.  
    I want to have the ability to cancel a scene loading operation because if the player encounters a network issue, I will make the player re-login the game, but I don't want to the player to see that he suddenly enters the game scene while still logging.
     
    Last edited: Jul 11, 2022
  9. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    70
    Unfortunately I am not sure, I WAS working in either Unity 2018 or 2019. It's very possible that they have since included a way to cancel an async scene load. From my memory, I couldn't tell that the scene had loaded and unloaded immediately, however, I wasn't working on a networked game, and we started our levels with a black screen fade, so if it was only up for a frame, it would just show black.
     
  10. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    I learnt from other thread mentioning the fact that setting activateOnLoad to false will block the main thread until the scene is loaded. So it's a risky move. Addressables still does not have the function to cancel any loading operation. However, I recently managed to write my own AssetBundleProvider and add the abort functionality. I can only abort the UnityWebRequest though, I can't abort uncompressing and loading currently.