Tanoda
LoadingManager.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using GILES;
4using UnityEngine;
6using UnityEngine.UI;
7
8public class LoadingManager : pb_MonoBehaviourSingleton<LoadingManager>
9{
10 [SerializeField] private Text loadingTitle;
11 [SerializeField] private Text loadingText;
12 [SerializeField] private RectTransform loadingImg;
13 [SerializeField] private GameObject loadingWindow;
14 [SerializeField] private CanvasGroup cg;
15 public float rotatingAngle = 10.0f;
16 public float timeout = 10.0f;
17 public bool MainLoading = false;
19
20 private Coroutine delayedHide, actualHide;
21
22 [HideInInspector] public bool isLoading = false;
23 internal bool FilesLoaded = false;
24
25 private void Start()
26 {
27 if (loadingText == null)
28 loadingText = gameObject.DemandComponent<Text>();
29 if (loadingTitle == null)
30 loadingTitle = gameObject.DemandComponent<Text>();
31 if (loadingWindow == null)
32 loadingWindow = new GameObject("temp");
33
35 }
36
37 private void OnEnable()
38 {
39 timeout = 10.0f;
40 }
41
42 public void ShowWindow()
43 {
44 if (actualHide != null)
45 {
46 StopCoroutine(actualHide);
47 }
48 isLoading = true;
49#if UNITY_WEBGL
50 cg.alpha = 1f;
51 cg.blocksRaycasts = true;
52 cg.interactable = true;
53#else
54 loadingWindow.SetActive(true);
55#endif
56#if !UNITY_WEBGL && TRILIB_2
57 if (delayedHide != null)
58 StopCoroutine(delayedHide);
59
60#endif
61 }
62 public void HideWindow(bool ignoreDelayedEvents = false)
63 {
64 if (actualHide != null)
65 {
66 StopCoroutine(actualHide);
67 }
68 actualHide = StartCoroutine(realHide(ignoreDelayedEvents));
69 }
70
71 IEnumerator realHide(bool ignoreDelayedEvents = false)
72 {
73 yield return new WaitForSeconds(2f);
74
75 isLoading = false;
76
77#if UNITY_WEBGL
78 cg.alpha = 0f;
79 cg.blocksRaycasts = false;
80 cg.interactable = false;
81#else
82#endif
83#if !UNITY_WEBGL && TRILIB_2
84 if (delayedHide != null)
85 StopCoroutine(delayedHide);
86 delayedHide = StartCoroutine(DelayedHide());
87#else
88 loadingWindow.SetActive(false);
89 onCompleated?.Invoke();
90 if (!ignoreDelayedEvents)
91 {
92 StartCoroutine(DelayedEvent());
93 }
94#endif
95 }
96
97 private IEnumerator DelayedHide()
98 {
99 yield return new WaitForSeconds(2.5f);
100 loadingWindow.SetActive(false);
101
102 onCompleated.Invoke();
103 StartCoroutine(DelayedEvent());
104 }
105
106 public void SetSyncing()
107 {
108 loadingText.text = Macro.T("SYNCING");
109 loadingTitle.text = Macro.T("SYNCING");
110 }
111 public void SetConnecting()
112 {
113 loadingText.text = Macro.T("CONNECTING");
114 loadingTitle.text = Macro.T("CONNECTING");
115 }
116
117 public void SetLoading()
118 {
119 loadingText.text = Macro.T("LOADING");
120 loadingTitle.text = Macro.T("LOADING");
121 }
122
123 IEnumerator DelayedEvent()
124 {
125 yield return new WaitForSeconds(2.5f);
126 onDelayedCompleated?.Invoke();
127 }
128
129 private void FixedUpdate()
130 {
131 if (!isLoading)
132 {
133 timeout = 10.0f;
134 }
135 if (!loadingImg) return;
136 loadingImg.eulerAngles = loadingImg.eulerAngles + Vector3.forward * rotatingAngle;
137 if (isLoading && timeout > 0)
138 {
139 timeout -= Time.fixedDeltaTime;
140 if (timeout <= 0)
141 {
142 onTimeOut.Invoke();
143 }
144 }
145 }
146}
UnityEvent onCompleated
UnityEvent onTimeOut
UnityEvent onDelayedCompleated
void HideWindow(bool ignoreDelayedEvents=false)
Definition: Macro.cs:12
static string T(string key)
Definition: Macro.cs:19