-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientLoadingScreen.cs
248 lines (211 loc) · 8.55 KB
/
ClientLoadingScreen.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
namespace Unity.Multiplayer.Samples.Utilities
{
/// <summary>
/// This script handles the use of a loading screen with a progress bar and the name of the loaded scene shown. It
/// must be started and stopped from outside this script. It also allows updating the loading screen when a new
/// loading operation starts before the loading screen is stopped.
/// </summary>
public class ClientLoadingScreen : MonoBehaviour
{
protected class LoadingProgressBar
{
public Slider ProgressBar { get; set; }
public Text NameText { get; set; }
public LoadingProgressBar(Slider otherPlayerProgressBar, Text otherPlayerNameText)
{
ProgressBar = otherPlayerProgressBar;
NameText = otherPlayerNameText;
}
public void UpdateProgress(float value, float newValue)
{
ProgressBar.value = newValue;
}
}
[SerializeField]
CanvasGroup m_CanvasGroup;
[SerializeField]
float m_DelayBeforeFadeOut = 0.5f;
[SerializeField]
float m_FadeOutDuration = 0.1f;
[SerializeField]
Slider m_ProgressBar;
[SerializeField]
Text m_SceneName;
[SerializeField]
List<Slider> m_OtherPlayersProgressBars;
[SerializeField]
List<Text> m_OtherPlayerNamesTexts;
[SerializeField]
protected LoadingProgressManager m_LoadingProgressManager;
protected Dictionary<ulong, LoadingProgressBar> m_LoadingProgressBars = new Dictionary<ulong, LoadingProgressBar>();
bool m_LoadingScreenRunning;
Coroutine m_FadeOutCoroutine;
void Awake()
{
DontDestroyOnLoad(this);
Assert.AreEqual(m_OtherPlayersProgressBars.Count, m_OtherPlayerNamesTexts.Count, "There should be the same number of progress bars and name labels");
}
void Start()
{
SetCanvasVisibility(false);
m_LoadingProgressManager.onTrackersUpdated += OnProgressTrackersUpdated;
}
void OnDestroy()
{
if (m_LoadingProgressManager != null)
{
m_LoadingProgressManager.onTrackersUpdated -= OnProgressTrackersUpdated;
}
}
void Update()
{
if (m_LoadingScreenRunning)
{
m_ProgressBar.value = m_LoadingProgressManager.LocalProgress;
}
}
void OnProgressTrackersUpdated()
{
// deactivate progress bars of clients that are no longer tracked
var clientIdsToRemove = new List<ulong>();
foreach (var clientId in m_LoadingProgressBars.Keys)
{
if (!m_LoadingProgressManager.ProgressTrackers.ContainsKey(clientId))
{
clientIdsToRemove.Add(clientId);
}
}
foreach (var clientId in clientIdsToRemove)
{
RemoveOtherPlayerProgressBar(clientId);
}
// Add progress bars for clients that are now tracked
foreach (var progressTracker in m_LoadingProgressManager.ProgressTrackers)
{
var clientId = progressTracker.Key;
if (clientId != NetworkManager.Singleton.LocalClientId && !m_LoadingProgressBars.ContainsKey(clientId))
{
AddOtherPlayerProgressBar(clientId, progressTracker.Value);
}
}
}
public void StopLoadingScreen()
{
if (m_LoadingScreenRunning)
{
if (m_FadeOutCoroutine != null)
{
StopCoroutine(m_FadeOutCoroutine);
}
m_FadeOutCoroutine = StartCoroutine(FadeOutCoroutine());
}
}
public void StartLoadingScreen(string sceneName)
{
SetCanvasVisibility(true);
m_LoadingScreenRunning = true;
UpdateLoadingScreen(sceneName);
ReinitializeProgressBars();
}
void ReinitializeProgressBars()
{
// deactivate progress bars of clients that are no longer tracked
var clientIdsToRemove = new List<ulong>();
foreach (var clientId in m_LoadingProgressBars.Keys)
{
if (!m_LoadingProgressManager.ProgressTrackers.ContainsKey(clientId))
{
clientIdsToRemove.Add(clientId);
}
}
foreach (var clientId in clientIdsToRemove)
{
RemoveOtherPlayerProgressBar(clientId);
}
for (var i = 0; i < m_OtherPlayersProgressBars.Count; i++)
{
m_OtherPlayersProgressBars[i].gameObject.SetActive(false);
m_OtherPlayerNamesTexts[i].gameObject.SetActive(false);
}
var index = 0;
foreach (var progressTracker in m_LoadingProgressManager.ProgressTrackers)
{
var clientId = progressTracker.Key;
if (clientId != NetworkManager.Singleton.LocalClientId)
{
UpdateOtherPlayerProgressBar(clientId, index++);
}
}
}
protected virtual void UpdateOtherPlayerProgressBar(ulong clientId, int progressBarIndex)
{
m_LoadingProgressBars[clientId].ProgressBar = m_OtherPlayersProgressBars[progressBarIndex];
m_LoadingProgressBars[clientId].ProgressBar.gameObject.SetActive(true);
m_LoadingProgressBars[clientId].NameText = m_OtherPlayerNamesTexts[progressBarIndex];
m_LoadingProgressBars[clientId].NameText.gameObject.SetActive(true);
}
protected virtual void AddOtherPlayerProgressBar(ulong clientId, NetworkedLoadingProgressTracker progressTracker)
{
if (m_LoadingProgressBars.Count < m_OtherPlayersProgressBars.Count && m_LoadingProgressBars.Count < m_OtherPlayerNamesTexts.Count)
{
var index = m_LoadingProgressBars.Count;
m_LoadingProgressBars[clientId] = new LoadingProgressBar(m_OtherPlayersProgressBars[index], m_OtherPlayerNamesTexts[index]);
progressTracker.Progress.OnValueChanged += m_LoadingProgressBars[clientId].UpdateProgress;
m_LoadingProgressBars[clientId].ProgressBar.value = progressTracker.Progress.Value;
m_LoadingProgressBars[clientId].ProgressBar.gameObject.SetActive(true);
m_LoadingProgressBars[clientId].NameText.gameObject.SetActive(true);
m_LoadingProgressBars[clientId].NameText.text = $"Client {clientId}";
}
else
{
throw new Exception("There are not enough progress bars to track the progress of all the players.");
}
}
void RemoveOtherPlayerProgressBar(ulong clientId, NetworkedLoadingProgressTracker progressTracker = null)
{
if (progressTracker != null)
{
progressTracker.Progress.OnValueChanged -= m_LoadingProgressBars[clientId].UpdateProgress;
}
m_LoadingProgressBars[clientId].ProgressBar.gameObject.SetActive(false);
m_LoadingProgressBars[clientId].NameText.gameObject.SetActive(false);
m_LoadingProgressBars.Remove(clientId);
}
public void UpdateLoadingScreen(string sceneName)
{
if (m_LoadingScreenRunning)
{
m_SceneName.text = sceneName;
if (m_FadeOutCoroutine != null)
{
StopCoroutine(m_FadeOutCoroutine);
}
}
}
void SetCanvasVisibility(bool visible)
{
m_CanvasGroup.alpha = visible ? 1 : 0;
m_CanvasGroup.blocksRaycasts = visible;
}
IEnumerator FadeOutCoroutine()
{
yield return new WaitForSeconds(m_DelayBeforeFadeOut);
m_LoadingScreenRunning = false;
float currentTime = 0;
while (currentTime < m_FadeOutDuration)
{
m_CanvasGroup.alpha = Mathf.Lerp(1, 0, currentTime / m_FadeOutDuration);
yield return null;
currentTime += Time.deltaTime;
}
SetCanvasVisibility(false);
}
}
}