Files

89 lines
2.2 KiB
C#
Raw Permalink Normal View History

2026-06-02 18:57:47 -04:00
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BracerLib.UI
{
public class SceneLoadingManager : MonoBehaviour
{
private static SceneLoadingManager _instance;
public static void LoadScenes(string[] scenePath, LoadSceneMode sceneMode)
{
if (!ValidInstance())
return;
_instance.TryLoadScenes(scenePath, sceneMode);
}
public static void Show()
{
if (ValidInstance())
_instance.Toggle(true);
}
public static void Hide()
{
if (ValidInstance())
_instance.Toggle(false);
}
private static bool ValidInstance()
{
if (_instance != null)
return true;
Debug.LogError("No Load Screen Manager found.");
return false;
}
[SerializeField]
private GameObject _loadingScreenGameObject;
private Coroutine _sceneLoadingCoroutine;
private void Awake()
{
DontDestroyOnLoad(_loadingScreenGameObject);
if (_instance != null)
{
Debug.LogError("Multiple LoadScreenManagers found. Please delete one", this);
Destroy(this);
return;
}
_instance = this;
}
private void TryLoadScenes(string[] scenePaths, LoadSceneMode sceneMode)
{
if (_sceneLoadingCoroutine != null)
{
Debug.LogError("SceneLoadingManager is already loading scenes.");
return;
}
_sceneLoadingCoroutine = StartCoroutine(LoadSceneCoroutine(scenePaths, sceneMode));
}
private IEnumerator LoadSceneCoroutine(string[] scenePaths, LoadSceneMode sceneMode)
{
Show();
for (var i = 0; i < scenePaths.Length; i++)
yield return SceneManager.LoadSceneAsync(scenePaths[i], sceneMode);
Hide();
_sceneLoadingCoroutine = null;
}
private void Toggle(bool state)
{
_loadingScreenGameObject.SetActive(state);
}
}
}