-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLevelLoader.cs
More file actions
89 lines (78 loc) · 2.2 KB
/
LevelLoader.cs
File metadata and controls
89 lines (78 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LevelLoader : MonoBehaviour
{
private bool triggered = false;
[SerializeField] private float fadeTime = 1f;
private Texture2D fadeTexture;
private float fadeAlpha = 0f;
private bool fading = false;
void Start()
{
fadeTexture = new Texture2D(1, 1);
fadeTexture.SetPixel(0, 0, Color.black);
fadeTexture.Apply();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
LoadNextLevel();
}
if (Input.GetKeyDown(KeyCode.R))
{
ReloadCurrentLevel();
}
if (Input.GetKeyDown(KeyCode.P))
{
LoadPrevLevel();
}
}
public void LoadPrevLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex - 1);
}
public void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex + 1);
}
public void ReloadCurrentLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Trigger entered by: " + other.name);
if (!triggered && other.CompareTag("Player"))
{
triggered = true;
StartCoroutine(StartFade());
}
}
IEnumerator StartFade()
{
fading = true;
float timer = 0f;
while (timer < fadeTime)
{
timer += Time.deltaTime;
fadeAlpha = Mathf.Clamp01(timer / fadeTime);
yield return null;
}
fadeAlpha = 1f;
LoadNextLevel();
}
void OnGUI()
{
if (fading && fadeAlpha > 0)
{
// Draw black texture over entire screen
GUI.color = new Color(0, 0, 0, fadeAlpha);
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
}
}