Add scene transitions
This commit is contained in:
61
Assets/Scripts/LevelChanger.cs
Normal file
61
Assets/Scripts/LevelChanger.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user