- using Eflatun.SceneReference;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- namespace MyCollections.SceneManagement
- {
- public class SceneLoader : MonoBehaviour
- {
- public SceneGroup[] sceneGroups;
- private static SceneLoader instance;
- [SerializeField] private GameObject loadingScreen;
- [SerializeField] private SceneReference loading;
-
- public static SceneLoader Instance => instance;
- private void Awake()
- {
- if (instance == null)
- {
- instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- private void Start()
- {
- }
- private SceneGroup FindSceneGroupByName(string name) =>
- Array.Find(sceneGroups, group => group.groupName == name);
- public void LoadScenes(string groupName, LoadType type)
- {
- SceneGroup sceneGroup = FindSceneGroupByName(groupName);
- switch (type)
- {
- case LoadType.Loading:
- StartCoroutine(LoadWithLoadingScreenAsync(sceneGroup));
- break;
- case LoadType.Trigger:
- LoadSceneGroup(groupName);
- break;
- }
- }
- public void LoadSceneTrigger(SceneReference sceneReference)
- {
- int count = SceneManager.sceneCount;
- for (int i = 0; i < count; i++)
- {
- Scene scene = SceneManager.GetSceneAt(i);
- if (scene.name == sceneReference.Name) return;
- }
- Debug.LogWarning("LoadingScene: " + sceneReference.Name);
- SceneManager.LoadSceneAsync(sceneReference.Name, LoadSceneMode.Additive);
- }
- public void UnloadSceneTrigger(SceneReference unloadScene, SceneReference newActiveScene)
- {
- int count = SceneManager.sceneCount;
- bool isSceneLoaded = false;
- for (int i = 0; i < count; i++)
- {
- Scene scene = SceneManager.GetSceneAt(i);
- if (scene.name == unloadScene.Name)
- {
- isSceneLoaded = true;
- break;
- }
- }
- if (!isSceneLoaded)
- {
- Debug.LogWarning("Scene to unload not loaded: " + unloadScene.Name);
- return;
- }
- SceneManager.UnloadSceneAsync(unloadScene.Name);
- SetActiveScene(newActiveScene);
- }
- private void SetActiveScene(SceneReference sceneReference)
- {
- int count = SceneManager.sceneCount;
- for (int i = 0; i < count; i++)
- {
- Scene scene = SceneManager.GetSceneAt(i);
- if (scene.name == sceneReference.Name)
- {
- SceneManager.SetActiveScene(scene);
- return;
- }
- }
- }
- public void LoadSceneWithLoading(SceneReference sceneToLoad, SceneReference sceneToUnload) =>
- StartCoroutine(LoadSceneWithLoadingScreen(sceneToLoad, sceneToUnload));
- public void LoadSceneGroup(string groupName)
- {
- SceneGroup group = FindSceneGroupByName(groupName);
- Debug.Log($"Group Found: {group.groupName}");
- if (group == null)
- return;
- List<string> scenesToLoad = new List<string>();
- int count = SceneManager.sceneCount;
- Debug.Log($"Scene Count: {count}");
- for (int i = 0; i < group.scenes.Count; i++)
- {
- bool isSceneLoaded = false;
- for (int j = 0; j < count; j++)
- {
- Scene loadedScene = SceneManager.GetSceneAt(j);
- if (loadedScene.name == group.scenes[i].Name)
- {
- isSceneLoaded = true;
- break;
- }
- }
- if (!isSceneLoaded)
- {
- Debug.Log($"Scene to Load Added: {group.scenes[i].Name}");
- scenesToLoad.Add(group.scenes[i].Name);
- }
- }
- StartCoroutine(LoadAndSetActiveScene(scenesToLoad, group));
- }
- private IEnumerator LoadSceneWithLoadingScreen(SceneReference sceneToLoad, SceneReference sceneToUnload)
- {
- string sceneName = loading.Name;
-
- AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
-
- yield return new WaitUntil(() => op.isDone);
-
- int s = SceneManager.sceneCount;
-
- GameObject loadingCanvas = GameObject.FindGameObjectWithTag("LoadingCanvas");
- loadingScreen = loadingCanvas.transform.GetChild(0).gameObject;
- if (loadingScreen == null)
- {
- Debug.LogError("Loading Screen is Null");
- yield break;
- }
-
- loadingScreen.SetActive(true);
- Animator animator;
-
- if (!loadingScreen.TryGetComponent(out animator))
- {
- Debug.LogError("Animator is Null");
- yield break;
- }
- AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
- float duration = stateInfo.length;
-
- yield return new WaitForSeconds(duration);
-
- SceneManager.UnloadSceneAsync(sceneToUnload.Name);
-
- AsyncOperation loadSceneOp = SceneManager.LoadSceneAsync(sceneToLoad.Name, LoadSceneMode.Additive);
-
- yield return new WaitUntil(() => loadSceneOp.isDone);
-
- animator.SetTrigger("FadeOut");
-
- stateInfo = animator.GetCurrentAnimatorStateInfo(0);
- duration = stateInfo.length;
-
- yield return new WaitForSeconds(duration);
-
-
- AsyncOperation unloadOp = SceneManager.UnloadSceneAsync(sceneName);
- yield return new WaitUntil(() => unloadOp.isDone);
- }
- private IEnumerator LoadWithLoadingScreenAsync(SceneGroup group)
- {
- List<string> scenesToLoad = new List<string>();
- string sceneName = loading.Name;
-
- AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
-
- yield return new WaitUntil(() => op.isDone);
-
- int s = SceneManager.sceneCount;
-
- GameObject loadingCanvas = GameObject.FindGameObjectWithTag("LoadingCanvas");
- loadingScreen = loadingCanvas.transform.GetChild(0).gameObject;
- if (loadingScreen == null)
- {
- Debug.LogError("Loading Screen is Null");
- yield break;
- }
-
- loadingScreen.SetActive(true);
- Animator animator;
-
- if (!loadingScreen.TryGetComponent(out animator))
- {
- Debug.LogError("Animator is Null");
- yield break;
- }
- AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
- float duration = stateInfo.length;
-
- yield return new WaitForSeconds(duration);
-
- for (int i = 0; i < group.scenes.Count; i++)
- {
- bool isSceneLoaded = false;
-
- int currentSceneCount = SceneManager.sceneCount;
- Debug.Log($"Current Scene Count: {currentSceneCount}");
- for (int j = 0; j < currentSceneCount; j++)
- {
-
- Scene loadedScene = SceneManager.GetSceneAt(j);
- Debug.Log($"LoadedScene: {loadedScene.name}");
-
- if (loadedScene.name == sceneName)
- {
- continue;
- }
-
- if (loadedScene.name == group.scenes[i].Name)
- {
-
- Debug.Log($"The Scene is Loaded: {loadedScene.name}");
- isSceneLoaded = true;
- break;
- }
- }
-
- if (!isSceneLoaded)
- {
- Debug.Log($"Scene to Load Added: {group.scenes[i].Name}");
- scenesToLoad.Add(group.scenes[i].Name);
- }
- }
-
- UnloadScene(group);
-
- for (int i = 0; i < scenesToLoad.Count; i++)
- {
- AsyncOperation loadSceneOp = SceneManager.LoadSceneAsync(scenesToLoad[i], LoadSceneMode.Additive);
-
- yield return new WaitUntil(() => loadSceneOp.isDone);
-
- SceneManager.SetActiveScene(SceneManager.GetSceneByName(scenesToLoad[i]));
- }
-
- animator.SetTrigger("FadeOut");
-
- stateInfo = animator.GetCurrentAnimatorStateInfo(0);
- duration = stateInfo.length;
-
- yield return new WaitForSeconds(duration);
-
-
- AsyncOperation unloadOp = SceneManager.UnloadSceneAsync(sceneName);
- yield return new WaitUntil(() => unloadOp.isDone);
- }
- private IEnumerator LoadAndSetActiveScene(List<string> scenesToLoad, SceneGroup group)
- {
- for (int i = 0; i < scenesToLoad.Count; i++)
- {
- AsyncOperation op = SceneManager.LoadSceneAsync(scenesToLoad[i], LoadSceneMode.Additive);
- Debug.Log($"Loading Scene: {scenesToLoad[i]}");
-
- yield return new WaitUntil(() => op.isDone);
-
- SceneManager.SetActiveScene(SceneManager.GetSceneByName(scenesToLoad[i]));
- Debug.Log($"Scene {scenesToLoad[i]} set as active.");
- }
-
- UnloadScene(group);
- }
- private void UnloadScene(SceneGroup group)
- {
- List<Scene> scenesToUnload = group.FindSceneRefByType(SceneType.DisableScene);
- Debug.Log($"Scenes Found To Unload: {scenesToUnload.Count}");
- List<string> unloadScenes = new List<string>();
- if (scenesToUnload.Count == 0)
- return;
- for (int i = 0; i < scenesToUnload.Count; i++)
- {
- Scene sceneToUnload = scenesToUnload[i];
- if (sceneToUnload.isLoaded)
- {
- Debug.Log($"Scene to Unload: {sceneToUnload.name}");
- unloadScenes.Add(sceneToUnload.name);
- }
- }
- if (unloadScenes.Count > 0)
- {
- for (int i = 0; i < unloadScenes.Count; i++)
- {
- SceneManager.UnloadSceneAsync(unloadScenes[i]);
- Debug.Log($"Unloading Scene: {unloadScenes[i]}");
- }
- }
- }
- }
- [Serializable]
- public struct SceneDataSaver
- {
-
- public Vector3 position;
- public string groupName;
- }
-
-
- public enum LoadType
- {
- Trigger,
- Loading
- }
- public enum SceneAction
- {
- LoadScene,
- UnloadScene
- }
- }