Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created February 23, 2018 21:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unity3dcollege/20ecf13a738258ff5bffdf8587c61a41 to your computer and use it in GitHub Desktop.
Save unity3dcollege/20ecf13a738258ff5bffdf8587c61a41 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
[SerializeField]
private string[] levelNames;
private ScreenWipe screenWipe;
private int nextLevelIndex;
private void Awake()
{
DontDestroyOnLoad(gameObject);
screenWipe = FindObjectOfType<ScreenWipe>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
StartCoroutine(LoadNextLevel());
}
private IEnumerator LoadNextLevel()
{
nextLevelIndex++;
if (nextLevelIndex >= levelNames.Length)
nextLevelIndex = 0;
string nextLevelName = levelNames[nextLevelIndex];
screenWipe.ToggleWipe(true);
while (!screenWipe.isDone)
yield return null;
AsyncOperation operation = SceneManager.LoadSceneAsync(nextLevelName);
while (!operation.isDone)
yield return null;
screenWipe.ToggleWipe(false);
}
}
using UnityEngine;
using UnityEngine.UI;
public class ScreenWipe : MonoBehaviour
{
[SerializeField]
[Range(0.1f, 3f)]
private float wipeSpeed = 1f;
private Image image;
private enum WipeMode { NotBlocked, WipingToNotBlocked, Blocked, WipingToBlocked }
private WipeMode wipeMode = WipeMode.NotBlocked;
private float wipeProgress;
public bool isDone { get; private set; }
private void Awake()
{
image = GetComponentInChildren<Image>();
DontDestroyOnLoad(gameObject);
}
public void ToggleWipe(bool blockScreen)
{
isDone = false;
if (blockScreen)
wipeMode = WipeMode.WipingToBlocked;
else
wipeMode = WipeMode.WipingToNotBlocked;
}
private void Update()
{
switch(wipeMode)
{
case WipeMode.WipingToBlocked:
WipeToBlocked();
break;
case WipeMode.WipingToNotBlocked:
WipeToNotBlocked();
break;
}
}
private void WipeToBlocked()
{
wipeProgress += Time.deltaTime * (1f / wipeSpeed);
image.fillAmount = wipeProgress;
if (wipeProgress >= 1f)
{
isDone = true;
wipeMode = WipeMode.Blocked;
}
}
private void WipeToNotBlocked()
{
wipeProgress -= Time.deltaTime * (1f / wipeSpeed);
image.fillAmount = wipeProgress;
if (wipeProgress <= 0)
{
isDone = true;
wipeMode = WipeMode.NotBlocked;
}
}
[ContextMenu("Block")]
private void Block() { ToggleWipe(true); }
[ContextMenu("Clear")]
private void Clear() { ToggleWipe(false); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment