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 Addressables Duplicates Additive Scenes from an Addressable menu scene

Discussion in 'Addressables' started by stevenchristian20, Oct 24, 2021.

  1. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    Hey So I have been working on with addressables for a mobile app for a little while and ran into an issue. The app is a mobile AR app that has a main scene for the viewer, but all the AR content in the scene is loaded as additive scenes using AssetReference. The way I have it structured is I have a button in the main scene that pulls an addressable DLC menu with buttons I dynamically load remotely. Each button uses LoadSceneAsync to load a new scene additively to the AR scene. If the scene is loaded while the DLC menu is active it will give you the option to unload and reload it. When I unload the DLC menu scene, I does not remember loading those scenes and they are essentially stuck in memory until the main AR scene is closes. When I reload the DLC menu, it will load another instance of the scene I added before instead of checking if the scene was already added.

    What I want to do is when I additively load the DLC menu scene, I want the button to check if the instance of that AssetReference scene is already loaded. If the scene is already loaded, then I want to be able to unload it rather than load another instance of that scene.

    Here is my current code for the button. It downloads the addressable scene, uses a progressbar for dl progress, and loads scene additively with text indication. I want it to check if the scene instance for the scene to be loaded already exists and make that existing scene instance the new scene instance for button to control.

    Code (CSharp):
    1.  
    2. public AssetReference arScene;
    3. public LoadSceneMode loadSceneMode;
    4. SceneInstance m_LoadedScene;
    5. bool m_ReadyToLoad = true;  
    6.  
    7.  
    8. void OnButtonClick()
    9.     {
    10.                 if(string.IsNullOrEmpty(sceneName))
    11.             Debug.LogError("Address To Add not set.");
    12.         else
    13.         {
    14.        
    15.             if (m_ReadyToLoad)
    16.             {
    17.                 textArea.text = loadingTxt;
    18.                 StartCoroutine(DownloadScene());
    19.             }
    20.             else
    21.             {
    22.                 Addressables.UnloadSceneAsync(m_LoadedScene).Completed += OnSceneUnloaded;
    23.             }
    24.            
    25.         }
    26.  
    27. IEnumerator DownloadScene()
    28.     {
    29.         yield return new WaitForSeconds(3);
    30.         var downloadscene = Addressables.LoadSceneAsync(arScene, loadSceneMode);
    31.         downloadscene.Completed += OnSceneLoaded;
    32.         Debug.LogError("Starting Download");
    33.        
    34.         while (!downloadscene.IsDone)
    35.         {
    36.             downloadProgressBar.SetActive(true);
    37.             yield return null;
    38.            
    39.         }
    40.         Debug.LogError("Download complete, starting scene");
    41.  
    42.     }
    43.  
    44.     void OnSceneLoaded(AsyncOperationHandle<SceneInstance> obj)
    45.     {
    46.         if (obj.Status == AsyncOperationStatus.Succeeded)
    47.         {
    48.             downloadProgressBar.SetActive(false);
    49.             textArea.text = unloadTxt + sceneName;
    50.             m_LoadedScene = obj.Result;
    51.             m_ReadyToLoad = false;
    52.         }
    53.         else
    54.         {
    55.             Debug.LogError("Failed to load scene at address: " + sceneName);
    56.         }
    57.     }
    58.  
    59.     private void OnSceneUnloaded(AsyncOperationHandle<SceneInstance> obj)
    60.     {
    61.         if (obj.Status == AsyncOperationStatus.Succeeded)
    62.         {
    63.  
    64.          
    65.             textArea.text = reloadTxt + sceneName;
    66.             m_ReadyToLoad = true;
    67.             m_LoadedScene = new SceneInstance();
    68.         }
    69.         else
    70.         {
    71.             Debug.LogError("Failed to unload scene at address: " + sceneName);
    72.         }
    73.     }
    74.  
    TLDR: I want my DLC menu button to check if there is an instance of my Addressable scene already loaded additively before trying to load it again. If it is, then attach that instance to my DLC button to unload it.

    Any help would be appreciated!