WinterJamSnowman/Assets/Scripts/LemonGenericLib/SceneManagement/SceneTransition.cs

131 lines
3.7 KiB
C#
Raw Permalink Normal View History

2023-01-24 13:51:46 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
namespace Lemon.GenericLib.SceneManagement {
public class SceneTransition : MonoBehaviour
{
public static SceneTransition Instance;
//singleton pattern
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
if (Instance != this)
{
Destroy(gameObject);
}
}
[SerializeField] float transitionTime;
//[SerializeField] UITransitionAnimator transitionAnimator;
[SerializeField] bool enableStartingTransition = true;
[SerializeField] UnityEvent transitionEnterFunction;
[SerializeField] UnityEvent transitionExitFunction;
private bool isTransitionAvailable = true;
// Start is called before the first frame update
void Start()
{
if (enableStartingTransition)
{
isTransitionAvailable = false;
transitionEnterFunction?.Invoke();
//transitionAnimator.TransitionIn();
StartCoroutine(EnableTransition());
}
}
public void TransitionScene(int sceneIndex)
{
if (!isTransitionAvailable) return;
isTransitionAvailable = false;
transitionExitFunction?.Invoke();
//transitionAnimator.TransitionOut();
StartCoroutine(LoadScene(sceneIndex));
}
public void TransitionScene(string sceneName)
{
if (!isTransitionAvailable) return;
isTransitionAvailable = false;
transitionExitFunction?.Invoke();
//transitionAnimator.TransitionOut();
StartCoroutine(LoadScene(sceneName));
}
public void RestartScene()
{
TransitionScene(SceneManager.GetActiveScene().buildIndex);
}
public void QuitGame()
{
transitionExitFunction?.Invoke();
StartCoroutine(QuitGameRoutine());
}
public void TransitionNextScene()
{
if (!isTransitionAvailable) return;
isTransitionAvailable = false;
transitionExitFunction?.Invoke();
//transitionAnimator.TransitionOut();
StartCoroutine(LoadScene(SceneManager.GetActiveScene().buildIndex + 1));
}
public void TransitionPreviousScene()
{
if (!isTransitionAvailable) return;
isTransitionAvailable = false;
transitionExitFunction?.Invoke();
//transitionAnimator.TransitionOut();
StartCoroutine(LoadScene(SceneManager.GetActiveScene().buildIndex - 1));
}
public void TransitionScene(int sceneIndex, bool useAnimation = true)
{
transitionExitFunction?.Invoke();
// transitionAnimator.TransitionOut();
StartCoroutine(LoadScene(sceneIndex));
}
IEnumerator LoadScene(int sceneIndex)
{
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(sceneIndex);
}
IEnumerator LoadScene(string sceneName)
{
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(sceneName);
}
IEnumerator EnableTransition()
{
yield return new WaitForSeconds(transitionTime);
isTransitionAvailable = true;
}
IEnumerator QuitGameRoutine()
{
yield return new WaitForSeconds(transitionTime);
Application.Quit();
}
}
}