Add scene transitions

This commit is contained in:
2022-08-13 04:57:40 -07:00
parent 9261aa640b
commit 61904c8068
8 changed files with 527 additions and 282 deletions

View File

@@ -0,0 +1,61 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Scampz.GameJam
{
public class LevelChanger : MonoBehaviour
{
public Animator animator;
public int sceneIndex = 0;
public static LevelChanger Singleton { get; private set; }
public void Update()
{
if (Input.GetMouseButtonDown(0))
FadeToLevel();
}
public void OnTriggerEnter(Collider collider)
{
FadeToLevel();
}
public void OnFadeComplete()
{
StartCoroutine(LoadScene(sceneIndex));
}
private void Awake()
{
if (Singleton is not null && Singleton != this)
Destroy(this);
else
Singleton = this;
}
private void FadeToLevel()
{
animator.SetTrigger("FadeOut");
}
private IEnumerator LoadScene(int levelIndex)
{
yield return null;
var loadSceneOperation = SceneManager.LoadSceneAsync(levelIndex);
loadSceneOperation.allowSceneActivation = false;
while (!loadSceneOperation.isDone)
{
if (loadSceneOperation.progress >= 0.9f)
break;
}
loadSceneOperation.allowSceneActivation = true;
yield return loadSceneOperation;
}
}
}