docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method UnloadSceneAsync

    UnloadSceneAsync(SceneInstance, UnloadSceneOptions, bool)

    Release scene

    Declaration
    public static AsyncOperationHandle<SceneInstance> UnloadSceneAsync(SceneInstance scene, UnloadSceneOptions unloadOptions, bool autoReleaseHandle = true)
    Parameters
    Type Name Description
    SceneInstance scene

    The SceneInstance to release.

    UnloadSceneOptions unloadOptions

    Specify behavior for unloading embedded scene objecs

    bool autoReleaseHandle

    If true, the handle will be released automatically when complete.

    Returns
    Type Description
    AsyncOperationHandle<SceneInstance>

    The operation handle for the scene unload.

    Remarks

    UnloadSceneAsync releases a previously loaded scene. The scene must have been activated to be unloaded.

    Passing UnloadSceneOptions.UnloadAllEmbeddedSceneObjects will unload assets embedded in the scene. The default is UploadSceneOptions.None which will only unload the scene's GameObjects.

    Examples
    // The idea with this sample is to show a simple streaming world. This has 3 events
    // that work to load and unload scenes as required. Load would be triggered when
    // the player gets close to the entrypoint. Activate would be called by doing
    // something like "opening a door". And then unload could be called when the player
    // crosses the entrypoint again in the other direction.
    public class SceneGatewayManager {
    
        // the Addressables key for the scene to load
        [SerializeField]
        public string sceneKey;
    
        private AsyncOperationHandle<SceneInstance> sceneHandle;
    
        public void EnterBoundary()
        {
            if (sceneHandle.IsValid() && sceneHandle.IsDone)
            {
                // do not reload if they have already passed the load boundary
                return;
            }
    
            // load, but do not activate
            var activateOnLoad = false;
    
            // the scene is additive to keep our base scene and simply add a new area to it
            sceneHandle = Addressables.LoadSceneAsync(sceneKey, LoadSceneMode.Additive, activateOnLoad);
        }
    
        public IEnumerator<AsyncOperationHandle<SceneInstance>> EnterScene()
        {
            // at this point we have to finish waiting for the scene to load if it
            // hasn't already
            yield return sceneHandle;
    
            // this will activate the scene
            sceneHandle.Result.ActivateAsync();
        }
    
        // exist boundary takes an optional cleanup callback that could be
        // used to cleanup Assets after scene unload
        public void ExitBoundary(Action cleanupCallback = null)
        {
    
            if (!sceneHandle.Result.Scene.isLoaded)
            {
                // scene has not been activated and cannot be unloaded
                return;
            }
            var unloadHandle = Addressables.UnloadSceneAsync(sceneHandle, UnloadSceneOptions.None);
            unloadHandle.Completed += (s) =>
            {                    
                if (cleanupCallback != null)
                {
                    cleanupCallback();
                }
            };
        }
    
    
    }

    UnloadSceneAsync(AsyncOperationHandle, UnloadSceneOptions, bool)

    Release scene

    Declaration
    public static AsyncOperationHandle<SceneInstance> UnloadSceneAsync(AsyncOperationHandle handle, UnloadSceneOptions unloadOptions, bool autoReleaseHandle = true)
    Parameters
    Type Name Description
    AsyncOperationHandle handle

    The handle returned by LoadSceneAsync for the scene to release.

    UnloadSceneOptions unloadOptions

    Specify behavior for unloading embedded scene objecs

    bool autoReleaseHandle

    If true, the handle will be released automatically when complete.

    Returns
    Type Description
    AsyncOperationHandle<SceneInstance>

    The operation handle for the scene unload.

    UnloadSceneAsync(SceneInstance, bool)

    Release scene

    Declaration
    public static AsyncOperationHandle<SceneInstance> UnloadSceneAsync(SceneInstance scene, bool autoReleaseHandle = true)
    Parameters
    Type Name Description
    SceneInstance scene

    The SceneInstance to release.

    bool autoReleaseHandle

    If true, the handle will be released automatically when complete.

    Returns
    Type Description
    AsyncOperationHandle<SceneInstance>

    The operation handle for the scene unload.

    UnloadSceneAsync(AsyncOperationHandle, bool)

    Release scene

    Declaration
    public static AsyncOperationHandle<SceneInstance> UnloadSceneAsync(AsyncOperationHandle handle, bool autoReleaseHandle = true)
    Parameters
    Type Name Description
    AsyncOperationHandle handle

    The handle returned by LoadSceneAsync for the scene to release.

    bool autoReleaseHandle

    If true, the handle will be released automatically when complete.

    Returns
    Type Description
    AsyncOperationHandle<SceneInstance>

    The operation handle for the scene unload.

    UnloadSceneAsync(AsyncOperationHandle<SceneInstance>, bool)

    Release scene

    Declaration
    public static AsyncOperationHandle<SceneInstance> UnloadSceneAsync(AsyncOperationHandle<SceneInstance> handle, bool autoReleaseHandle = true)
    Parameters
    Type Name Description
    AsyncOperationHandle<SceneInstance> handle

    The handle returned by LoadSceneAsync for the scene to release.

    bool autoReleaseHandle

    If true, the handle will be released automatically when complete.

    Returns
    Type Description
    AsyncOperationHandle<SceneInstance>

    The operation handle for the scene unload.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)