Search Unity

[0.7.5] cant unload scene

Discussion in 'Addressables' started by HuangWM, May 9, 2019.

  1. HuangWM

    HuangWM

    Joined:
    Nov 3, 2015
    Posts:
    45
    throw exception when unload scene:
    upload_2019-5-9_9-59-1.png

    This exception because not add scene handle to m_resultToHandle at AddressablesImpl.OnSceneHandleCompleted
    upload_2019-5-9_10-0-47.png

    Hope to fix it
     
  2. alicht

    alicht

    Joined:
    Jan 22, 2019
    Posts:
    6
    I stumbled over the same thing yesterday. The fix was obviously to keep the handle referenced instead of the SceneInstance. I would be curious though for which use case the
    Addressables.UnloadSceneAsync(SceneInstance instance)
    API is designed for when it removes the internal dictionary entrance right after loading is completed.
     
  3. 4sascha

    4sascha

    Joined:
    Mar 9, 2017
    Posts:
    51
    Can you give me some more insights how you solved the problem?

    Here is a simple example from the Adressables examples.

    https://github.com/Unity-Technologi...aster/Basic/Scene Loading/Assets/AddScenes.cs

    who throws same exeption.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.ResourceManagement.ResourceProviders;
    using UnityEngine.SceneManagement;
    using UnityEngine.Serialization;
    using UnityEngine.UI;
    public class AddScenes : MonoBehaviour
    {
    public string addressToAdd;
    Button m_AddButton;
    public TMPro.TMP_Text textArea;
    SceneInstance m_LoadedScene;
    bool m_ReadyToLoad = true;
    // Use this for initialization
    void Start () {
    m_AddButton = GetComponent<Button>();
    m_AddButton.onClick.AddListener(OnButtonClick);
    textArea.text = "Load " + addressToAdd;
    }
    void OnButtonClick()
    {
    if(string.IsNullOrEmpty(addressToAdd))
    Debug.LogError("Address To Add not set.");
    else
    {
    if (m_ReadyToLoad)
    Addressables.LoadSceneAsync(addressToAdd, LoadSceneMode.Additive).Completed += OnSceneLoaded;
    else
    {
    Addressables.UnloadSceneAsync(m_LoadedScene).Completed += OnSceneUnloaded;
    }
    }
    }
    void OnSceneUnloaded(AsyncOperationHandle<SceneInstance> obj)
    {
    if (obj.Status == AsyncOperationStatus.Succeeded)
    {
    textArea.text = "Reload " + addressToAdd;
    m_ReadyToLoad = true;
    m_LoadedScene = new SceneInstance();
    }
    else
    {
    Debug.LogError("Failed to unload scene at address: " + addressToAdd);
    }
    }
    void OnSceneLoaded(AsyncOperationHandle<SceneInstance> obj)
    {
    if (obj.Status == AsyncOperationStatus.Succeeded)
    {
    textArea.text = "Unload " + addressToAdd;
    m_LoadedScene = obj.Result;
    m_ReadyToLoad = false;
    }
    else
    {
    Debug.LogError("Failed to load scene at address: " + addressToAdd);
    }
    }
    }


    Here is another thread about this problem
    https://forum.unity.com/threads/0-7-4-unloadscene-problem.666907/
     
    Last edited: May 15, 2019
  4. alicht

    alicht

    Joined:
    Jan 22, 2019
    Posts:
    6
    Instead of storing the scene instance, you can keep the whole operation handle and use it to unload:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AddressableAssets;
    3. using UnityEngine.ResourceManagement.AsyncOperations;
    4. using UnityEngine.ResourceManagement.ResourceProviders;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7.  
    8. public class AddScenes : MonoBehaviour
    9.  
    10. {
    11.     public string addressToAdd;
    12.     public TMPro.TMP_Text textArea;
    13.  
    14.     Button m_AddButton;
    15.    
    16.     AsyncOperationHandle<SceneInstance> m_handle;
    17.     Scene m_scene;
    18.  
    19.     bool m_ReadyToLoad = true;
    20.    
    21.     void Start()
    22.     {
    23.         m_AddButton = GetComponent<Button>();
    24.         m_AddButton.onClick.AddListener(OnButtonClick);
    25.         textArea.text = "Load " + addressToAdd;
    26.     }
    27.  
    28.     void OnButtonClick()
    29.     {
    30.         if (string.IsNullOrEmpty(addressToAdd))
    31.             Debug.LogError("Address To Add not set.");
    32.         else
    33.         {
    34.             if (m_ReadyToLoad)
    35.             {
    36.                 m_handle = Addressables.LoadSceneAsync(addressToAdd, LoadSceneMode.Additive);
    37.                 m_handle.Completed += OnSceneLoaded;
    38.                 m_ReadyToLoad = false;
    39.             }
    40.             else if (m_handle.IsValid())
    41.             {
    42.                 Addressables.UnloadSceneAsync(m_handle).Completed += OnSceneUnloaded;
    43.                 m_handle = default(AsyncOperationHandle<SceneInstance>);
    44.             }
    45.         }
    46.     }
    47.  
    48.     void OnSceneUnloaded(AsyncOperationHandle<SceneInstance> obj)
    49.     {
    50.         if (obj.Status == AsyncOperationStatus.Succeeded)
    51.         {
    52.             textArea.text = "Reload " + addressToAdd;
    53.             m_ReadyToLoad = true;
    54.             m_scene = default(Scene);
    55.         }
    56.         else
    57.         {
    58.             Debug.LogError("Failed to unload scene at address: " + addressToAdd);
    59.         }
    60.     }
    61.  
    62.     void OnSceneLoaded(AsyncOperationHandle<SceneInstance> obj)
    63.     {
    64.         if (obj.Status == AsyncOperationStatus.Succeeded)
    65.         {
    66.             textArea.text = "Unload " + addressToAdd;
    67.             m_scene = obj.Result.Scene;
    68.         }
    69.         else
    70.         {
    71.             Debug.LogError("Failed to load scene at address: " + addressToAdd);
    72.         }
    73.     }
    74. }
     
    4sascha likes this.
  5. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    fixed in 0.8.6-preview
     
    alicht likes this.