-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTravelManager.cs
More file actions
97 lines (76 loc) · 2.52 KB
/
TravelManager.cs
File metadata and controls
97 lines (76 loc) · 2.52 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
90
91
92
93
94
95
96
97
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
public class TravelManager : MonoBehaviour
{
public GameObject travelMenu;
public Image loadingIcon;
public float fadeDuration = 0.5f;
public void OpenTravelMenu()
{
StartCoroutine(ShowPanel(travelMenu));
}
public void CloseTravelMenu()
{
StartCoroutine(HidePanel(travelMenu));
}
public void TravelToScene(string sceneName)
{
StartCoroutine(HidePanel(travelMenu, () => StartCoroutine(LoadSceneAsync(sceneName))));
}
IEnumerator LoadSceneAsync(string sceneName)
{
StartCoroutine(AnimateLoadingIcon());
yield return new WaitForSeconds(1);
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
operation.allowSceneActivation = false;
while (!operation.isDone)
{
if (operation.progress >= 0.9f)
{
yield return new WaitForSeconds(1);
operation.allowSceneActivation = true;
}
yield return null;
}
}
IEnumerator AnimateLoadingIcon()
{
{
loadingIcon.transform.Rotate(0, 0, -200 * Time.deltaTime);
yield return null;
}
}
IEnumerator ShowPanel(GameObject panel)
{
panel.SetActive(true);
CanvasGroup canvasGroup = panel.GetComponent<CanvasGroup>();
if (canvasGroup == null) canvasGroup = panel.AddComponent<CanvasGroup>();
float elapsedTime = 0;
float duration = 0.3f;
while (elapsedTime < duration)
{
canvasGroup.alpha = Mathf.Lerp(0, 1, elapsedTime / duration);
elapsedTime += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 1;
}
IEnumerator HidePanel(GameObject panel, System.Action onComplete = null)
{
CanvasGroup canvasGroup = panel.GetComponent<CanvasGroup>();
if (canvasGroup == null) canvasGroup = panel.AddComponent<CanvasGroup>();
float elapsedTime = 0;
float duration = 0.3f;
while (elapsedTime < duration)
{
canvasGroup.alpha = Mathf.Lerp(1, 0, elapsedTime / duration);
elapsedTime += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 0;
panel.SetActive(false);
onComplete?.Invoke();
}
}