-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TutorialManager.cs
396 lines (332 loc) · 10.6 KB
/
TutorialManager.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using SweatyChair.UI;
namespace SweatyChair
{
/// <summary>
/// A manager to manage all tutorials.
/// </summary>
public static class TutorialManager
{
public static event UnityAction coreTutorialsChanged;
public static event UnityAction<Tutorial> tutorialCompleted;
public static Tutorial currentTutorial;
public static TutorialSettings settings => TutorialSettings.current;
public static List<Tutorial> tutorials => settings.tutorials;
public static bool isTutorialRunning => currentTutorial != null;
public static bool isFirstTutorialCompleted => tutorials[0].isCompleted;
public static bool areAllTutorialsCompleted {
get {
foreach (Tutorial t in tutorials) {
if (!t.isCompleted)
return false;
}
return true;
}
}
#region Core Tutorials
public static bool areCoreTutorialsCompleted {
get {
foreach (Tutorial t in tutorials) {
if (!t.isCore)
continue;
if (!t.isCompleted)
return false;
}
return true;
}
}
public static int totalCoreTutorials {
get {
int i = 0;
foreach (Tutorial t in tutorials) {
if (!t.isCore)
continue;
i++;
}
return i;
}
}
public static int totalCoreTutorialsCompleted {
get {
int i = 0;
foreach (Tutorial t in tutorials) {
if (!t.isCore)
continue;
if (!t.isCompleted)
continue;
i++;
}
return i;
}
}
#endregion
static TutorialManager()
{
GameSaveSync.gameSaveUpdated += CheckTutorialCompletedForReturnPlayers;
}
/// <summary>
/// Check if the tutorial already completed for return players, after a latest game service saved game downloaded.
/// </summary>
public static void CheckTutorialCompletedForReturnPlayers()
{
if (areAllTutorialsCompleted) return;
foreach (Tutorial t in tutorials)
t.CheckCompleteForReturnPlayers();
coreTutorialsChanged?.Invoke();
}
public static void StartTutorial(int id)
{
if (isTutorialRunning) // Already running a tutorial, skip for now
return;
if (areAllTutorialsCompleted) // Either all tutorials has been completed or never instantiated
return;
Tutorial t = GetTutorial(id);
if (t != null && !t.isCompleted)
currentTutorial = t;
StartCurrentTutorial();
}
public static void StartCurrentTutorial()
{
if (currentTutorial != null) {
if (string.IsNullOrEmpty(currentTutorial.sceneName) || SceneManager.GetActiveScene().name == currentTutorial.sceneName) // No scene need to load
currentTutorial.StartTutorial();
else
TimeManager.Start(LoadCurrentTutorialSceneCoroutine());
}
}
private static IEnumerator LoadCurrentTutorialSceneCoroutine()
{
if (currentTutorial == null)
yield break;
int currentSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
AsyncOperation async = SceneManager.LoadSceneAsync(currentTutorial.sceneName);
while (!async.isDone)
yield return null;
currentTutorial.StartTutorial();
Scene scene = SceneManager.GetSceneByName(currentTutorial.sceneName);
SceneManager.SetActiveScene(scene);
async = SceneManager.UnloadSceneAsync(currentSceneBuildIndex);
while (!async.isDone)
yield return null;
}
/// <summary>
/// Starts the tutorial task by index on the current tutorial step of the current tutorial.
/// </summary>
/// <param name="taskIndex">index of tutorial instance task</param>
public static void StartCurrentTutorialTask(int taskIndex)
{
if (currentTutorial != null)
currentTutorial.StartTask(taskIndex);
else
Debug.LogErrorFormat("TutorialManager:StartCurrentTutorialStep - Could not start task {0}, currentTutorial=null", taskIndex);
}
/// <summary>
/// Completes the tutorial task by index on the current tutorial step of the current tutorial.
/// </summary>
/// <param name="taskIndex">index of tutorial instance task</param>
public static void CompleteCurrentTutorialTask(int taskIndex)
{
if (currentTutorial != null)
currentTutorial.CompleteTask(taskIndex);
else
Debug.LogErrorFormat("TutorialManager::CompleteCurrentTutorialTask - Could not complete task {0}, currentTutorial=null", taskIndex);
}
/// <summary>
/// Completes the current tutorial instance.
/// </summary>
public static void CompleteCurrentTutorialStep()
{
CompleteCurrentTutorialSteps(1);
}
/// <summary>
/// Completes a number of current tutorial instances.
/// </summary>
/// <param name="stepIndex">count of tutorial instances</param>
public static void CompleteCurrentTutorialSteps(int count)
{
if (currentTutorial != null) {
currentTutorial.CompleteSteps(count);
} else {
// Tutorial gameobject may be dragged out for debug directly in Editor mode, we try to find it here
TutorialStep ts = Object.FindObjectOfType<TutorialStep>();
if (ts == null)
Debug.LogError("TutorialManager::CompleteCurrentTutorialSteps - Failed, currentTutorial=null");
else
ts.Complete();
}
}
public static Tutorial GetCoreTutorialByIndex(int index)
{
List<Tutorial> coreTutorials = new List<Tutorial>();
foreach (Tutorial t in tutorials) {
if (t.isCore)
coreTutorials.Add(t);
}
if (index < 0)
index += coreTutorials.Count;
if (index < 0 || index >= coreTutorials.Count) {
Debug.LogFormat("TutorialManager:GetTutorial - Invalid index={0}", index);
return null;
}
return coreTutorials[index];
}
public static Tutorial GetTutorial(int id)
{
foreach (Tutorial t in tutorials) {
if (t.id == id)
return t;
}
Debug.LogFormat("TutorialManager:GetTutorial - Invalid id={0}", id);
return null;
}
public static Tutorial GetTutorial(string prefabName)
{
foreach (Tutorial t in tutorials) {
if (t.prefab.name == prefabName)
return t;
}
Debug.LogFormat("TutorialManager:GetTutorial - Invalid prefabName={0}", prefabName);
return null;
}
public static Tutorial GetTutorial(TutorialValidator validator)
{
foreach (Tutorial t in tutorials) {
if (t.validator == validator)
return t;
}
Debug.LogFormat("TutorialManager:GetTutorial - Invalid validator={0}", validator);
return null;
}
public static void AbortCurrentTutorial()
{
if (currentTutorial == null) {
Debug.LogError("TutorialManager:EndCurrentTutorial - currentTutorial=null, ignore this if debuuging in Editor mode");
return;
}
CompleteCurrentTutorial();
//TutorialHintController.DestroyAll();
TutorialPanel.DestroyInstance();
}
public static void CompleteCurrentTutorial(bool doTutorialComplete = true)
{
if (currentTutorial == null) {
Debug.LogError("TutorialManager:CompleteCurrentTutorial - currentTutorial=null, ignore this if debuging in Editor.");
return;
}
if (currentTutorial.validator != null && doTutorialComplete)
currentTutorial.validator.OnTutorialComplete();
if (currentTutorial.resetOnComplete)
currentTutorial.Reset();
if (!settings.debugMode)
currentTutorial.CompleteTutorial();
tutorialCompleted?.Invoke(currentTutorial);
AnalyticsTutorialComplete(currentTutorial.id);
currentTutorial = null;
}
public static void CompleteTutorial(int id)
{
Tutorial tutorial = GetTutorial(id);
if (tutorial != null)
tutorial.isCompleted = true;
}
public static void CompleteTutorial(TutorialValidator validator)
{
Tutorial tutorial = GetTutorial(validator);
if (tutorial != null)
tutorial.isCompleted = true;
}
public static bool IsTutorialCompleted(int id)
{
if (areAllTutorialsCompleted)
return true;
return GetTutorial(id).isCompleted;
}
public static bool IsTutorialRunning(int id)
{
return currentTutorial != null && currentTutorial.id == id && currentTutorial.isRunning;
}
private static bool IsTutorialRunning(Tutorial tutorial)
{
return currentTutorial == tutorial && currentTutorial.isRunning;
}
public static void SetCurrentTutorialStep(TutorialStep tutorialStep)
{
if (currentTutorial == null) {
Debug.LogError("TutorialManager:SetCurrentTutorialStep - currentTutorial=null, ignore this if debuuging in Editor mode");
return;
}
currentTutorial.SetCurrentStep(tutorialStep);
}
public static List<TutorialValidator> GetAllValidators()
{
List<TutorialValidator> tmp = new List<TutorialValidator>();
foreach (Tutorial t in tutorials) {
if (t != null && t.validator != null)
tmp.Add(t.validator);
}
return tmp;
}
public static void CompleteManualSteps()
{
if (currentTutorial != null && currentTutorial.isRunning) {
CompleteManualSteps(currentTutorial.currentStep);
return;
}
#if UNITY_EDITOR
// For development only when TutorialStep is drag to edit without using TutorialManager and currentTutorial=null
TutorialStep ts = Object.FindObjectOfType<TutorialStep>();
if (ts != null)
CompleteManualSteps(ts);
#endif
}
private static void CompleteManualSteps(TutorialStep ts)
{
foreach (TutorialTask tt in ts.tasks) {
if (tt.completeTrigger == TutoriaCompletelTrigger.Manual)
tt.DoComplete();
}
}
public static void FireCompleteTrigger(TutoriaCompletelTrigger trigger)
{
if (currentTutorial != null && currentTutorial.isRunning) {
FireCompleteTrigger(currentTutorial.currentStep, trigger);
return;
}
#if UNITY_EDITOR
// For development only when TutorialStep is drag to edit without using TutorialManager and currentTutorial=null
TutorialStep ts = Object.FindObjectOfType<TutorialStep>();
if (ts != null)
FireCompleteTrigger(ts, trigger);
#endif
}
private static void FireCompleteTrigger(TutorialStep ts, TutoriaCompletelTrigger trigger)
{
if (ts != null) {
foreach (TutorialTask tt in ts.tasks)
tt.ReceiveCompleteTrigger(trigger);
}
}
// Analytics, add other analytics if needed
public static void AnalyticsTutorialStart(int tutorialId)
{
//Debug.LogFormat("GameAnalytics:TutorialStart({0})", tutorialId);
UnityEngine.Analytics.AnalyticsEvent.TutorialStart(tutorialId.ToString());
}
public static void AnalyticsTutorialComplete(int tutorialId)
{
//Debug.LogFormat("GameAnalytics:TutorialComplete({0})", tutorialId);
TimeManager.Invoke(() => {
UnityEngine.Analytics.AnalyticsEvent.TutorialComplete(tutorialId.ToString());
}, 1); // Send 1s later to avoid sending with tutorial step complete
}
public static void AnalyticsTutorialStep(int tutorialId, int stepIndex)
{
//Debug.LogFormat("GameAnalytics:TutorialStep({0},{1})", tutorialId, stepIndex);
UnityEngine.Analytics.AnalyticsEvent.TutorialStep(stepIndex, tutorialId.ToString());
}
}
}