Skip to content

Instantly share code, notes, and snippets.

@FNGgames
Last active October 12, 2023 01:32
Show Gist options
  • Save FNGgames/313a740c7652fabd3c24eaf205010808 to your computer and use it in GitHub Desktop.
Save FNGgames/313a740c7652fabd3c24eaf205010808 to your computer and use it in GitHub Desktop.
Scene Loader
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine.SceneManagement;
public static class SceneHelpers
{
public static string[] GetValidSceneNames()
{
int sceneCount = SceneManager.sceneCountInBuildSettings;
var scenes = new string[sceneCount];
for (int i = 0; i < sceneCount; i++)
scenes[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
return scenes;
}
public static Dictionary<string, int> GetSceneNameToBuildIndexMap()
{
int sceneCount = SceneManager.sceneCountInBuildSettings;
Dictionary<string, int> result = new Dictionary<string, int>();
for (int i = 0; i < sceneCount; i++)
result.Add(Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i)), i);
return result;
}
}
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneLoader : MonoBehaviour
{
public float fadeOutTime = 2;
public float fadeInTime = 2;
public Image fadeImage;
private Contexts _contexts;
private Scene _rootScene;
private readonly HashSet<string> _validScenes = new HashSet<string>();
public static SceneLoader Instance;
void Awake()
{
Instance = this;
DontDestroyOnLoad(gameObject);
_rootScene = SceneManager.GetActiveScene();
_validScenes.AddRange(SceneHelpers.GetValidSceneNames());
InitializeScene(_rootScene);
}
private void InitializeScene(Scene scene)
{
// scene initialization code here
foreach (var rootGameObject in scene.GetRootGameObjects())
{
// gameobject initialization
}
}
public void LoadScene(string sceneName, bool unloadExisting)
{
if (!_validScenes.Contains(sceneName)) throw new Exception(string.Format("Scene {0} is not defined in build settings"), sceneName);
StartCoroutine(SwitchScenes(sceneName, unloadExisting));
}
private IEnumerator SwitchScenes(string newScene, bool unloadExisting)
{
if (unloadExisting)
{
var unloadTarget = SceneManager.GetActiveScene();
if (fadeImage != null)
{
var fadeOut = DOTween.Sequence();
fadeOut.SetEase(Ease.Linear);
fadeOut.Append(fadeImage.DOFade(1, fadeOutTime));
fadeOut.Play();
while (fadeOut.IsPlaying()) yield return null;
}
if (unloadTarget != null)
{
var unload = SceneManager.UnloadSceneAsync(unloadTarget);
while (!unload.isDone) yield return null;
}
}
var loadTarget = "Scenes/" + newScene;
var load = SceneManager.LoadSceneAsync(loadTarget, LoadSceneMode.Additive);
while (!load.isDone) yield return null;
var newSceneReference = SceneManager.GetSceneByName(newScene);
SceneManager.SetActiveScene(newSceneReference);
InitializeScene(newSceneReference);
if (fadeImage == null) yield break;
var fadeIn = DOTween.Sequence();
fadeIn.SetEase(Ease.Linear);
fadeIn.Append(fadeImage.DOFade(0, fadeInTime));
fadeIn.Play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment