/// <summary>
        /// Unloads a single scene.
        /// </summary>
        /// <param name="scene">The scene to unload.</param>
        /// <param name="onComplete">Optional callback.</param>
        public static void UnloadScene(string scene, Action onComplete = null)
        {
            //Block flow if the scene does not exist
            if (!Application.CanStreamedLevelBeLoaded(scene))
            {
                if (LogLevel >= LogType.Less)
                {
                    Debug.LogWarning(Tag + "The scene \"" + scene + "\" cannot be found or does not exist.");
                }
                return;
            }

            //Block flow if the scene is not loaded
            Scene sceneObj = SceneManager.GetSceneByName(scene);

            if (!sceneObj.isLoaded)
            {
                if (LogLevel >= LogType.All)
                {
                    Debug.LogWarning(Tag + "The scene \"" + scene + "\" is not loaded.");
                }
                return;
            }

            AsyncOperation op = SceneManager.UnloadSceneAsync(scene);

            op.completed += _ => {
                onComplete?.Invoke();
                OnSceneUnloaded?.Invoke(new [] { scene });
            };
        }
        private void InvokeUnloadedActions(List <string> sceneNames, SceneType sceneType)
        {
            foreach (string sceneName in sceneNames)
            {  // Announce scenes individually regardless of type
                OnSceneUnloaded?.Invoke(sceneName);
            }

            switch (sceneType)
            {
            case SceneType.Content:
                // Announce content as a set
                OnContentUnloaded?.Invoke(sceneNames);
                break;

            case SceneType.Lighting:
                // We only handle lighting scenes one at a time
                Debug.Assert(sceneNames.Count == 1);
                OnLightingUnloaded?.Invoke(sceneNames[0]);
                break;

            default:
                // Don't announce other types of scenes invidually
                break;
            }
        }
Esempio n. 3
0
        private void InvokeUnloadedActions(List <string> sceneNames, SceneType sceneType)
        {
            try
            {
                foreach (string sceneName in sceneNames)
                {  // Announce scenes individually regardless of type
                    OnSceneUnloaded?.Invoke(sceneName);
                }

                switch (sceneType)
                {
                case SceneType.Content:
                    // Announce content as a set
                    OnContentUnloaded?.Invoke(sceneNames);
                    break;

                case SceneType.Lighting:
                    // We only handle lighting scenes one at a time
                    Debug.Assert(sceneNames.Count == 1);
                    OnLightingUnloaded?.Invoke(sceneNames[0]);
                    break;

                default:
                    // Don't announce other types of scenes invidually
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.LogError("Error when attempting to invoke unloaded actions for " + string.Join(", ", sceneNames));
                Debug.LogException(e);
            }
        }
Esempio n. 4
0
 private IEnumerator TriggerCallback(AsyncOperation unloadSceneAsync, int sceneId)
 {
     if (!unloadSceneAsync.isDone)
     {
         yield return(null);
     }
     OnSceneUnloaded?.Invoke(sceneId);
 }
Esempio n. 5
0
        private static void SceneUnloadedHandler(Scene scene)
        {
            if (AllAvailableGameObjectsByScene.ContainsKey(scene.name))
            {
                AllAvailableGameObjectsByScene.Remove(scene.name);
            }

            OnSceneUnloaded?.Invoke(scene.name);
        }
 private void SceneManager_OnSceneLoaded(Scene arg0, LoadSceneMode arg1)
 {
     Detach();
     _active = false;
     OnSceneUnloaded?.Invoke();
     if (arg0.buildIndex == SceneBuildIndex)
     {
         Attach(arg0);
         _active = true;
         OnSceneLoaded?.Invoke();
     }
 }
        /// <summary>
        /// Unloads an array of scenes.
        /// </summary>
        /// <param name="scenes">The scenes to unload.</param>
        /// <param name="onComplete">Optional callback.</param>
        public static void UnloadScenes(string[] scenes, Action onComplete = null)
        {
            AsyncOperation[] operations = new AsyncOperation[scenes.Length];

            for (var i = 0; i < scenes.Length; i++)
            {
                string scene = scenes[i];

                //Block flow if the scene does not exist
                if (!Application.CanStreamedLevelBeLoaded(scene))
                {
                    if (LogLevel >= LogType.Less)
                    {
                        Debug.LogWarning(Tag + "The scene \"" + scene + "\" cannot be found or does not exist.");
                    }
                    continue;
                }

                //Block flow if the scene is not loaded
                Scene sceneObj = SceneManager.GetSceneByName(scene);
                if (!sceneObj.isLoaded)
                {
                    if (LogLevel >= LogType.All)
                    {
                        Debug.LogWarning(Tag + "The scene \"" + scene + "\" is not loaded.");
                    }
                    continue;
                }

                AsyncOperation op = SceneManager.UnloadSceneAsync(scene);
                operations[i] = op;
            }

            //Attach the callback to last scene
            for (int i = operations.Length - 1; i >= 0; i--)
            {
                AsyncOperation op = operations[i];

                if (op == null)
                {
                    continue;
                }

                op.completed += _ => {
                    onComplete?.Invoke();
                    OnSceneUnloaded?.Invoke(scenes);
                };
                break;
            }
        }
        /// <summary>
        /// Unloads all scenes except for the provided array.
        /// </summary>
        /// <param name="scenesExcept">The list of scenes to not unload</param>
        /// <param name="onComplete">Optional callback.</param>
        /// <returns></returns>
        public static IEnumerator CoroutineUnloadAllScenesExcept(string[] scenesExcept, Action onComplete = null)
        {
            string[]      scenesToUnload = new string[SceneManager.sceneCount];
            List <string> unloadedScenes = new List <string>();

            //Loop through all of the existing scenes
            for (int i = 0; i < SceneManager.sceneCount; i++)
            {
                Scene scene = SceneManager.GetSceneAt(i);
                unloadedScenes.Add(scene.name);

                //Skip unloading if the scene is excluded
                bool flagSkip = false;
                foreach (string sceneExcept in scenesExcept)
                {
                    if (scene.name.Equals(sceneExcept))
                    {
                        flagSkip = true;
                        break;
                    }
                }
                if (flagSkip)
                {
                    continue;
                }

                scenesToUnload[i] = scene.name;
            }

            foreach (string scene in scenesToUnload)
            {
                if (scene == null)
                {
                    continue;
                }

                AsyncOperation op = SceneManager.UnloadSceneAsync(scene);

                while (!op.isDone)
                {
                    yield return(null);
                }
                //Debug
                //yield return new WaitForSeconds(1f);
            }

            onComplete?.Invoke();
            OnSceneUnloaded?.Invoke(unloadedScenes.ToArray());
        }
        /// <summary>
        /// Unloads all scenes except for the provided array.
        /// </summary>
        /// <param name="scenesExcept">The list of scenes to not unload.</param>
        /// <param name="onComplete">Optional callback.</param>
        public static void UnloadAllScenesExcept(string[] scenesExcept, Action onComplete = null)
        {
            int sceneCount = SceneManager.sceneCount;

            AsyncOperation[] operations     = new AsyncOperation[sceneCount];
            List <string>    unloadedScenes = new List <string>();

            //Loop through all of the existing scenes
            for (int i = 0; i < sceneCount; i++)
            {
                Scene scene = SceneManager.GetSceneAt(i);
                unloadedScenes.Add(scene.name);

                //Skip unloading if the scene is excluded
                bool flagSkip = false;
                foreach (string sceneExcept in scenesExcept)
                {
                    if (scene.name.Equals(sceneExcept))
                    {
                        flagSkip = true;
                        break;
                    }
                }
                if (flagSkip)
                {
                    continue;
                }

                AsyncOperation op = SceneManager.UnloadSceneAsync(scene);
                operations[i] = op;
            }

            //Attach the callback to last scene
            for (int i = operations.Length - 1; i >= 0; i--)
            {
                AsyncOperation op = operations[i];

                if (op == null)
                {
                    continue;
                }

                op.completed += _ => {
                    onComplete?.Invoke();
                    OnSceneUnloaded?.Invoke(unloadedScenes.ToArray());
                };
                break;
            }
        }
        /// <summary>
        /// Unloads an array of scenes.
        /// </summary>
        /// <param name="scenes">The scenes to unload.</param>
        /// <param name="onComplete">Optional callback.</param>
        /// <returns></returns>
        public static IEnumerator CoroutineUnloadScenes(string[] scenes, Action onComplete = null)
        {
            foreach (string scene in scenes)
            {
                //Block flow if the scene does not exist
                if (!Application.CanStreamedLevelBeLoaded(scene))
                {
                    if (LogLevel >= LogType.Less)
                    {
                        Debug.LogWarning(Tag + "The scene \"" + scene + "\" cannot be found or does not exist.");
                    }
                    continue;
                }

                //Block flow if the scene is not loaded
                Scene sceneObj = SceneManager.GetSceneByName(scene);
                if (!sceneObj.isLoaded)
                {
                    if (LogLevel >= LogType.All)
                    {
                        Debug.LogWarning(Tag + "The scene \"" + scene + "\" is not loaded.");
                    }
                    continue;
                }

                //Block flow if their is only one scene left
                if (SceneManager.sceneCount == 1)
                {
                    if (LogLevel >= LogType.Less)
                    {
                        Debug.LogWarning(Tag + "The scene \"" + scene + "\" is the last scene. Cant unload the last scene.");
                    }
                    continue;
                }

                AsyncOperation op = SceneManager.UnloadSceneAsync(scene);

                while (!op.isDone)
                {
                    yield return(null);
                }
                //Debug
                //yield return new WaitForSeconds(1f);
            }

            onComplete?.Invoke();
            OnSceneUnloaded?.Invoke(scenes);
        }
 /// <summary>
 /// Destroy the currently loaded scene. Does nothing if no scene is loaded.
 /// </summary>
 public void destroyScene()
 {
     if (currentScene != null)
     {
         if (OnSceneUnloading != null)
         {
             OnSceneUnloading.Invoke(this, currentScene);
         }
         currentSimObjects.Dispose();
         currentSimObjects = null;
         currentScene.Dispose();
         currentScene = null;
         if (OnSceneUnloaded != null)
         {
             OnSceneUnloaded.Invoke(this, null);
         }
     }
 }
Esempio n. 12
0
        internal static void Internal_OnSceneEvent(SceneEventType eventType, Scene scene, ref Guid sceneId)
        {
            switch (eventType)
            {
            case SceneEventType.OnSceneSaving: OnSceneSaving?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneSaved: OnSceneSaved?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneSaveError: OnSceneSaveError?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneLoading: OnSceneLoading?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneLoaded: OnSceneLoaded?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneLoadError: OnSceneLoadError?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneUnloading: OnSceneUnloading?.Invoke(scene, sceneId); break;

            case SceneEventType.OnSceneUnloaded: OnSceneUnloaded?.Invoke(scene, sceneId); break;
            }
        }
Esempio n. 13
0
 public void destroyScene()
 {
     if (scene != null)
     {
         foreach (DebugInterface debugInterface in controller.PluginManager.DebugInterfaces)
         {
             debugInterface.destroyDebugInterface(controller.PluginManager.RendererPlugin, scene.getDefaultSubScene());
         }
         if (OnSceneUnloading != null)
         {
             OnSceneUnloading.Invoke(this, scene);
         }
         scene.Scope.Dispose();
         scene.Dispose();
         scene = null;
         if (OnSceneUnloaded != null)
         {
             OnSceneUnloaded.Invoke(this, null);
         }
     }
 }
Esempio n. 14
0
 public void SetSceneListener(OnSceneLoaded onSceneLoaded, OnSceneUnloaded onSceneUnloaded)
 {
     this.onSceneLoaded   = onSceneLoaded;
     this.onSceneUnloaded = onSceneUnloaded;
 }