UnloadScene() private method

private UnloadScene ( Scene scene ) : bool
scene Scene
return bool
Beispiel #1
0
        /// <summary>
        /// Unload a scene loaded via AddScene
        /// </summary>
        public void UnloadAddedScene(string sceneName)
        {
            for (int i = 0; i < mScenesAdded.Count; i++)
            {
                if (mScenesAdded[i].name == sceneName)
                {
                    mScenesAdded.RemoveAt(i);
                    UnitySceneManager.UnloadScene(sceneName);
                    return;
                }
            }

            //check the queue
            if (mScenesToAdd.Contains(sceneName))
            {
                //reconstruct the queue excluding the sceneName
                var newSceneQueue = new Queue <string>();
                while (mScenesToAdd.Count > 0)
                {
                    var s = mScenesToAdd.Dequeue();
                    if (s != sceneName)
                    {
                        newSceneQueue.Enqueue(s);
                    }
                }
                mScenesToAdd = newSceneQueue;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Unloads all added scenes loaded via AddScene
        /// </summary>
        public void UnloadAddedScenes()
        {
            for (int i = 0; i < mScenesAdded.Count; i++)
            {
                UnitySceneManager.UnloadScene(mScenesAdded[i]);
            }

            ClearAddSceneData();
        }
        private IEnumerator Preload()
        {
            // Scene, (Mod, object name, object id)
            Dictionary <string, List <(Mod, string, string)> > toPreload = new();

            foreach (Mod mod in Mod.ModList)
            {
                Logger.API.Log("Fetching preloads from mod " + mod.LogName);
                List <(string, string, string)> modPreloads;
                try
                {
                    modPreloads = mod.GetPreloads();
                }
                catch (Exception e)
                {
                    mod.Log("Failed getting preload names\n" + e);
                    continue;
                }

                foreach ((string scene, string obj, string id) in modPreloads)
                {
                    if (scene == null || obj == null || id == null)
                    {
                        Logger.API.Log($"Given null argument in preload ({scene ?? "null"}, {obj ?? "null"}, {id ?? "null"}), skipping");
                    }

                    if (!toPreload.TryGetValue(scene, out List <(Mod, string, string)> scenePreloads))
                    {
                        scenePreloads    = new();
                        toPreload[scene] = scenePreloads;
                    }

                    scenePreloads.Add((mod, obj, id));
                }
            }

            progressIndicator.gameObject.SetActive(true);
            progressIndicator.minValue = 0;
            progressIndicator.maxValue = toPreload.Count + 1;

            int preloadIdx = 0;

            foreach (string sceneName in toPreload.Keys)
            {
                Logger.API.Log("Preloading objects in scene " + sceneName);

                AsyncOperation loadop = USceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
                while (!loadop.isDone)
                {
                    progressIndicator.value = preloadIdx + loadop.progress / .9f;
                    yield return(null);
                }

                Scene        scene       = USceneManager.GetSceneByName(sceneName);
                GameObject[] rootObjects = scene.GetRootGameObjects();

                foreach ((Mod mod, string objPath, string objId) in toPreload[sceneName])
                {
                    string rootName;
                    string childName;
                    if (objPath.Contains('/'))
                    {
                        int slash = objPath.IndexOf('/');
                        if (slash == 0 || slash == objPath.Length - 1)
                        {
                            Logger.API.Log($"Malformatted object path '{objPath}' given by mod {mod.LogName}, skipping");
                            continue;
                        }

                        rootName  = objPath.Substring(0, slash);
                        childName = objPath.Substring(slash + 1);
                    }
                    else
                    {
                        rootName  = objPath;
                        childName = null;
                    }

                    GameObject obj = rootObjects.FirstOrDefault(o => o.name == rootName);
                    if (childName != null && obj != null)
                    {
                        Transform transform = obj.transform.Find(childName);
                        if (transform != null)
                        {
                            obj = transform.gameObject;
                        }
                    }

                    if (obj == null)
                    {
                        Logger.API.Log($"Couldn't find object with path '{objPath}' requested by mod {mod.LogName}");
                        continue;
                    }

                    obj = Instantiate(obj);
                    DontDestroyOnLoad(obj);
                    obj.SetActive(false);

                    try
                    {
                        mod.SetPreload(objId, obj);
                    }
                    catch (Exception e)
                    {
                        mod.Log($"Failed setting preloaded object '{objId}'\n{e}");
                        continue;
                    }

                    Logger.API.Log($"Successfully preloaded object '{objId}' for mod {mod.LogName}");
                }

                USceneManager.UnloadScene(scene);
                preloadIdx++;
            }

            AsyncOperation menuLoadop = USceneManager.LoadSceneAsync(Constants.MENU_SCENE);

            while (!menuLoadop.isDone)
            {
                progressIndicator.value = preloadIdx + menuLoadop.progress / .9f;
                yield return(null);
            }
        }
Beispiel #4
0
 public static bool UnloadScene(string sceneName)
 {
     sceneName = getBuildSettingsScenePath(sceneName);
     return(SceneManager.UnloadScene(sceneName));
 }
Beispiel #5
0
 public static bool UnloadScene(int sceneBuildIndex)
 {
     return(SceneManager.UnloadScene(sceneBuildIndex));
 }
Beispiel #6
0
 public static bool UnloadScene(Scene scene)
 {
     return(SceneManager.UnloadScene(scene));
 }
Beispiel #7
0
        IEnumerator DoLoadScene(string toScene, LoadSceneMode mode, bool unloadCurrent)
        {
            isLoading = true;

            //make sure the scene is not paused
            Resume();

            //play out transitions
            for (int i = 0; i < mTransitions.Count; i++)
            {
                yield return(mTransitions[i].Out());
            }

            //scene is about to change
            if (sceneChangeCallback != null)
            {
                sceneChangeCallback(toScene);
            }

            bool doLoad = true;

            if (mode == LoadSceneMode.Additive)
            {
                //Debug.Log("unload: "+mCurScene);
                if (unloadCurrent && mCurScene != mRootScene)
                {
                    UnitySceneManager.UnloadScene(mCurScene);

                    //unload added scenes
                    UnloadAddedScenes();
                }

                //load only if it doesn't exist
                doLoad = !UnitySceneManager.GetSceneByName(toScene).IsValid();
            }
            else
            {
                //single mode removes all other scenes
                ClearAddSceneData();
            }

            //load
            if (doLoad)
            {
                var sync = UnitySceneManager.LoadSceneAsync(toScene, mode);

                while (!sync.isDone)
                {
                    yield return(null);
                }
            }
            else
            {
                yield return(null);
            }

            mCurScene = UnitySceneManager.GetSceneByName(toScene);
            UnitySceneManager.SetActiveScene(mCurScene);

            if (sceneChangePostCallback != null)
            {
                sceneChangePostCallback();
            }

            //play in transitions
            for (int i = 0; i < mTransitions.Count; i++)
            {
                yield return(mTransitions[i].In());
            }

            isLoading = false;
        }