Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to check if scene is already loaded when using Addressables.LoadSceneAsync

Discussion in 'Addressables' started by inkletom, Nov 5, 2020.

  1. inkletom

    inkletom

    Joined:
    Oct 13, 2016
    Posts:
    63
    Hey! We have an AssetReference for a scene, and we want to check if the scene is already loaded before calling Addressables.LoadSceneAsync.
    As far as I can tell, this simply isn't possible because
    RuntimeKey in AssetReference is a GUID and there's no way to get the scene's name from it.
    Is this right?
    If so, it seems that the best approach is to use the Addressable Name as declared in the editor, and make sure it matches the file name - which removes one of the big advantages of using the system.
     
    ArthurClark likes this.
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    Hey there! I'll flag this for the team, and share any guidance they have!
     
  3. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    762
    Hey, so this is an interesting use case. The best way to go about it would be to load the IResourceLocation from the AssetReference and then use the InternalId of the location of the scene to ask the scene manager if the scene is loaded. Something like this (disclaimer this has only been tested in exactly one scenario and appeared to work):

    Code (CSharp):
    1. public AssetReference sceneReference;
    2.     public IEnumerator Start()
    3.     {
    4.         var loc = Addressables.LoadResourceLocationsAsync(sceneReference);
    5.         yield return loc;
    6.         var result = loc.Result;
    7.         Debug.Log(SceneManager.GetSceneByPath(result[0].InternalId).isLoaded);
    8.     }
    Hopefully this, or something very like it, will work for you.
     
  4. ThePilgrim

    ThePilgrim

    Joined:
    Apr 25, 2013
    Posts:
    17
    This seems to work for me, thanks! Here's the code I used to load a scene if it wasn't already loaded.

    Code (CSharp):
    1. [SerializeField] private AssetReference _scene = default;
    2. [SerializeField] private LoadSceneMode _loadMode = default;
    3.  
    4. public void LoadSceneIfNotLoaded()
    5. {
    6.     Addressables.LoadResourceLocationsAsync(_scene).Completed += (loc) =>
    7.     {
    8.         var isSceneLoaded = SceneManager.GetSceneByPath(loc.Result[0].InternalId).isLoaded;
    9.         if (!isSceneLoaded)
    10.         {
    11.             _scene.LoadSceneAsync(_loadMode);
    12.         }
    13.     };
    14. }
     
    trnq1ll0, ArthurClark and Pawciu like this.
  5. stalhandske

    stalhandske

    Joined:
    Mar 17, 2014
    Posts:
    24
    Was looking for exactly this and this answer worked. Thanks.
     
  6. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    809
    Hi! For the sake of understanding, what does InternalId return here, and why does it work as a scene path even though it's not in the build settings?
    Would this work with IResourceLocation.PrimaryKey as well?