Esempio n. 1
0
        IEnumerator _Load(string name)
        {
            void CalculateProgress(AsyncOperation curJob, int _totalStages, ref float _prevProgress, ref float _curProgress)
            {
                _curProgress += curJob.progress / _totalStages - _prevProgress;
                _prevProgress = curJob.progress / _totalStages;

                _curProgress = Mathf.Clamp(_curProgress, 0f, 1f);
                OnSceneLoading(_curProgress);
            }

            routines.Default.StopAll();
            OnSceneClose();
            ProcessorEntities.Clean();
            Toolbox.changingScene = true;
            Toolbox.Instance.ClearSessionData();

            //Plus two for unload assets and load target scene
            int   totalStagesNeed = 0;
            float curProgress = 0f, prevProgress = 0f;

            AsyncOperation job            = null;
            List <string>  scenesToUnload = new List <string>();

            //Add main scene
            scenesToUnload.Add(SceneManager.GetActiveScene().name);
            //Add additive scenes
            scenesToUnload.AddRange(sceneDependsOn);
            //Exclude scenes to keep
            scenesToUnload.RemoveAll((scene) => scenesToKeep.Contains(scene));

            totalStagesNeed = scenesToUnload.Count + 2;

            //Do all work
            for (int i = 0; i < totalStagesNeed; i++)
            {
                //Unload scenes
                if (i < scenesToUnload.Count)
                {
                    string key = scenesToUnload[i];

                    job = SceneManager.UnloadSceneAsync(scenes[key]);
                }
                //Cleaning
                else if (i < totalStagesNeed - 1)
                {
                    job = Resources.UnloadUnusedAssets();
                }
                //Load target scene
                else if (i < totalStagesNeed)
                {
                    scenes.Clear();
                    job = SceneManager.LoadSceneAsync(name, LoadSceneMode.Additive);
                }

                //Calculate progress
                while (!job.isDone)
                {
                    CalculateProgress(job, totalStagesNeed, ref prevProgress, ref curProgress);
                    yield return(0);
                }

                CalculateProgress(job, totalStagesNeed, ref prevProgress, ref curProgress);
                prevProgress = 0f;
            }

            SceneManager.SetActiveScene(SceneManager.GetSceneByName(name));
            job.allowSceneActivation = true;
            Toolbox.changingScene    = false;
        }
Esempio n. 2
0
        IEnumerator _Load(int id)
        {
            routines.Default.StopAll();
            ProcessorEntities.Clean();
            OnSceneClose();
            Toolbox.changingScene = true;
            Toolbox.Instance.ClearSessionData();

            if (scenes.Keys.Count == 1)
            {
                scenes.Clear();

                var jScene = SceneManager.LoadSceneAsync(id);
                while (!jScene.isDone)
                {
                    yield return(0);
                }
                SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(id));
                Toolbox.changingScene = false;
                yield break;
            }

            var s     = SceneManager.GetActiveScene();
            var sName = s.name;

            var job = SceneManager.UnloadSceneAsync(s);

            while (!job.isDone)
            {
                yield return(0);
            }


            scenes.Remove(sName);

            if (scenes.Keys.Count != 1)
            {
                foreach (var key in scenes.Keys)
                {
                    if (scenesToKeep.Contains(key))
                    {
                        continue;
                    }

                    job = SceneManager.UnloadSceneAsync(scenes[key]);

                    while (!job.isDone)
                    {
                        yield return(0);
                    }
                }

                job = Resources.UnloadUnusedAssets();
                while (!job.isDone)
                {
                    yield return(0);
                }
            }


            scenes.Clear();

            job = SceneManager.LoadSceneAsync(id, LoadSceneMode.Additive);
            while (!job.isDone)
            {
                yield return(0);
            }

            SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(id));
            job.allowSceneActivation = true;
            Toolbox.changingScene    = false;
        }