Files
Scampz/Assets/Scripts/LevelChanger.cs
2022-08-13 04:57:40 -07:00

62 lines
1.2 KiB
C#

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;
}
}
}