public void LoadScenes(StreamingAction action, string[] scenes, string sceneToActivate, bool showUI, Callable[] onLoadComplete)
        {
            if (EnableDebug)
            {
                DebugText.gameObject.SetActive(true);
            }

            List <string> requiredScenes = new List <string>();

            foreach (string scene in scenes)
            {
                if (
                    (SceneManager.GetSceneByName(scene).isLoaded&& action == StreamingAction.Unload) ||
                    (!SceneManager.GetSceneByName(scene).isLoaded&& action == StreamingAction.Load)
                    )
                {
                    requiredScenes.Add(scene);
                }
            }

            int count = requiredScenes.Count;

            percentages     = new float[count];
            asyncOperations = new AsyncOperation[count];

            if (showUI)
            {
                LoadingRoot.SetActive(true);
            }

            if (count > 0)
            {
                StartCoroutine(LoadScenesCoroutine(action, requiredScenes, sceneToActivate, showUI, onLoadComplete));
            }
            else
            {
                Debug.LogWarning("Did not find any candidates to load or unload...");

                if (onLoadComplete != null)
                {
                    Callable.Call(onLoadComplete);
                }

                if (showUI)
                {
                    LoadingRoot.SetActive(false);
                }

                if (EnableDebug)
                {
                    DebugText.gameObject.SetActive(false);
                }
            }
        }
Exemple #2
0
        IEnumerator LoadScenesCoroutine(StreamingAction action, List <string> scenes, string sceneToActivate, bool showUI, UnityEvent onLoadComplete)
        {
            LogDebugInformation("START LOAD/UNLOAD FOR LEVELS...");
            SetProgressBar(0.0f, true);

            switch (action)
            {
            case StreamingAction.Load:
                LogDebugInformation("[*] ASYNC LOAD FOR: " + scenes);
                StartCoroutine(LoadLevelCoroutine(scenes));
                break;

            case StreamingAction.Unload:
                LogDebugInformation("[*] ASYNC UNLOAD FOR: " + scenes);
                StartCoroutine(UnloadLevelCoroutine(scenes));
                break;

            default: throw new NotImplementedException("LoadScenesCoroutine does not handle mode " + action.ToString());
            }

            // Wait for all scenes to be loaded
            while (asyncOperations.Any(a => !a.isDone))
            {
                yield return(new WaitForEndOfFrame());
            }

            // Then change active scene
            if (sceneToActivate != "")
            {
                var newActive = SceneManager.GetSceneByName(sceneToActivate);
                SceneManager.SetActiveScene(newActive);
                yield return(new WaitForEndOfFrame());
            }

            if (onLoadComplete != null)
            {
                onLoadComplete.Invoke();
            }

            if (showUI)
            {
                LoadingRoot.SetActive(false);
            }

            if (EnableDebug)
            {
                DebugText.gameObject.SetActive(false);
            }
        }
        IEnumerator LoadScenesCoroutine(StreamingAction action, List <string> scenes, string sceneToActivate, bool showUI, Callable[] onLoadComplete)
        {
            LogDebugInformation("START LOAD/UNLOAD FOR LEVELS...");
            LoadingText.text = "Loading...";
            SetProgressBar(0.0f, true);
            yield return(new WaitForEndOfFrame());

            if (DelayBeforeLoad >= 0.0f)
            {
                yield return(new WaitForSeconds(DelayBeforeLoad));
            }

            int count = scenes.Count;

            percentages     = new float[count];
            asyncOperations = new AsyncOperation[count];

            switch (action)
            {
            case StreamingAction.Replace:
                LogDebugInformation("[*] ASYNC REPLACE FOR: " + scenes);
                StartCoroutine(LoadLevelCoroutine(scenes, sceneToActivate));
                break;

            case StreamingAction.Load:
                LogDebugInformation("[*] ASYNC LOAD FOR: " + scenes);
                StartCoroutine(LoadLevelCoroutine(scenes));
                break;

            case StreamingAction.Unload:
                LogDebugInformation("[*] ASYNC UNLOAD FOR: " + scenes);
                StartCoroutine(UnloadLevelCoroutine(scenes));
                break;

            default: throw new NotImplementedException("LoadScenesCoroutine does not handle mode " + action.ToString());
            }

            if (action == StreamingAction.Replace)
            {
                while (!asyncOperations[0].isDone)
                {
                    yield return(new WaitForEndOfFrame());
                }
            }

            // Wait for all scenes to be loaded
            while (asyncOperations.Any(a => !a.isDone))
            {
                yield return(new WaitForEndOfFrame());
            }

            // Then change active scene
            if (!string.IsNullOrEmpty(sceneToActivate) && action != StreamingAction.Replace)
            {
                var newActive = SceneManager.GetSceneByName(sceneToActivate);
                SceneManager.SetActiveScene(newActive);
                yield return(new WaitForEndOfFrame());
            }

            if (DelayAfterLoad >= 0.0f)
            {
                SetProgressBar(1.0f, true);
                yield return(new WaitForSeconds(DelayAfterLoad));
            }

            if (onLoadComplete != null)
            {
                Callable.Call(onLoadComplete);
            }

            if (showUI)
            {
                LoadingRoot.SetActive(false);
            }

            if (LoadingIcon != null)
            {
                LoadingIcon.SetActive(false);
            }

            if (EnableDebug)
            {
                DebugText.gameObject.SetActive(false);
            }
        }