-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TutorialPanel.cs
279 lines (236 loc) · 7.59 KB
/
TutorialPanel.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
using UnityEngine;
using UnityEngine.UI;
namespace SweatyChair.UI
{
/// <summary>
/// A tutorial panel, this is instantiated in runtime.
/// </summary>
public class TutorialPanel : Panel
{
// Prefab path in Resources folder
public const string RESOURCES_PREFAB_PATH = "TutorialPanel";
private static TutorialPanel _instance;
// Cached canvas used for comparing to new canvas and deciding whether to create a new Tutorial Panel.
private static Canvas _cachedCanvas;
// Parts GameObjects
[SerializeField] private GameObject _backgroundMaskGO, _handGO, _characterGO;
// Background Mask
[SerializeField] private Image _backgroundImage;
// Hand
[SerializeField] private RectTransform _handHolderRF;
[SerializeField] private Animation _handAnimation;
// Content
[SerializeField] private RectTransform _textRF;
[SerializeField] private Text _contentText;
// Character dialogue
[SerializeField] private RawImage _characterImage;
[SerializeField] private Text _characterText;
[SerializeField] private Animation _characterAnim;
private string _defaultHandAnimationClipName;
// Get TutorialPanel instance, instantiate one if there has no tutorial panel been created.
public static TutorialPanel current {
get {
if (_instance != null) {
return _instance;
} else {
#if UNITY_EDITOR
// In editor, try find the already spawned panel first
TutorialPanel tmp = FindObjectOfType<TutorialPanel>();
if (tmp != null)
return tmp;
#endif
_cachedCanvas = null;
if (!string.IsNullOrEmpty(TutorialSettings.current.canvasPath)) { // Find the specified Canvas
_cachedCanvas = TransformUtils.GetComponentByPath<Canvas>(TutorialSettings.current.canvasPath);
} else if (PanelManager.instance != null) {
_cachedCanvas = PanelManager.instance.GetComponent<Canvas>();
}
if (_cachedCanvas == null) // Try find the default Canvas object
_cachedCanvas = TransformUtils.GetComponentByPath<Canvas>("Canvas");
if (_cachedCanvas == null) // Find the first Canvas
_cachedCanvas = FindObjectOfType<Canvas>();
if (_cachedCanvas != null) {
GameObject go = _cachedCanvas.gameObject.AddChild(RESOURCES_PREFAB_PATH, false);
if (go != null) {
go.name = "TutorialPanel"; // Remove "(Clone)"
_instance = go.GetComponent<TutorialPanel>();
return _instance;
}
} else {
Debug.LogError("TutorialPanel:current - Cannot find Canvas");
}
return null;
}
}
}
public override void Init()
{
base.Init();
_defaultHandAnimationClipName = _handAnimation.clip.name;
InitTransform();
// All default off
ToggleBackgroundMask(false);
ToggleHand(false);
SetText(string.Empty);
ToggleCharacter(false);
}
/// <summary>
/// Creates a tutorial panel on the Canvas that the target is rendering onto, else gets TutorialPanel instance or instantiates one on the first found Canvas.
/// </summary>
/// <param name="targetTF">Target Transform to create a TutorialPanel for.</param>
public static TutorialPanel GetCurrent(Transform targetTF)
{
if (targetTF != null) {
Canvas targetCanvas = targetTF.GetComponentInParent<Canvas>();
if (targetCanvas != _cachedCanvas) {
GameObject go = targetCanvas.gameObject.AddChild(RESOURCES_PREFAB_PATH);
if (go)
return go.GetComponent<TutorialPanel>();
}
}
return current;
}
public override void Toggle(bool doShow)
{
base.Toggle(doShow);
if (!doShow) {
ToggleBackgroundMask(false);
ToggleHand(false);
ToggleCharacter(false);
} else {
InitTransform();
}
}
private void InitTransform()
{
// When instantiating prefabs using Tutorial, all rect transform settings are messed up. So here we are fixing that
RectTransform selfRectTF = transform as RectTransform;
if (selfRectTF == null) {
Debug.LogErrorFormat("'{0}' : Unable to set rectTransform size, our transform is not a rect transform", gameObject.name);
return;
}
// Reset our scale
selfRectTF.localScale = Vector3.one;
// Plus our anchored position
selfRectTF.anchorMin = Vector2.zero;
selfRectTF.anchorMax = Vector2.one;
selfRectTF.anchoredPosition = Vector2.zero;
selfRectTF.sizeDelta = Vector2.zero;
selfRectTF.offsetMin = Vector2.zero;
selfRectTF.offsetMax = Vector2.zero;
}
#region Background Mask and Spotlight
public void ToggleBackgroundMask(bool isShown, int alpha = -1)
{
if (_backgroundImage == null)
return;
_backgroundMaskGO.SetActive(isShown);
if (isShown) {
if (alpha == -1)
alpha = 168;
_backgroundImage.color = new Color(0, 0, 0, alpha / 255f);
}
}
public Button GetBackgroundButton()
{
if (_backgroundImage != null)
return _backgroundImage.GetComponent<Button>();
return null;
}
#endregion
#region Hand
public void ToggleHand(bool isShown)
{
_handGO.SetActive(isShown);
}
public void SetHandPosition(Vector3 handPosition)
{
_handHolderRF.position = handPosition;
}
public Vector3 GetHandLocalPosition()
{
return _handHolderRF.localPosition;
}
public void SetHandTransform(Vector3 handLocalPosition, Vector3 handLocalRotation, Vector3 handLocalScale)
{
_handHolderRF.localPosition = handLocalPosition;
_handHolderRF.localRotation = Quaternion.Euler(handLocalRotation);
_handHolderRF.localScale = handLocalScale;
_handHolderRF.SetAsLastSibling();
}
public void SetHandAnimation(string clipName)
{
if (string.IsNullOrEmpty(clipName))
return;
TimeManager.Start(AnimationUtils.Play(_handAnimation, clipName, false));
}
#endregion
#region Text
public void SetTextPosition(Vector3 textPosition)
{
_textRF.position = textPosition;
}
public Vector3 GetTextLocalPosition()
{
return _textRF.localPosition;
}
public void SetTextTransform(Vector3 textLocalPosition, Vector3 textLocalRotation, Vector2 textSize)
{
_textRF.gameObject.SetActive(true); //Make sure the gameobject is enabled if string was empty before.
_textRF.localPosition = textLocalPosition;
_textRF.localRotation = Quaternion.Euler(textLocalRotation);
_textRF.sizeDelta = textSize;
_textRF.SetAsLastSibling();
}
public void SetText(string text)
{
_contentText.text = LocalizeUtils.Get(TermCategory.Tutorial, text);
}
public void SetText(string text, Color color, int fontSize, TextAnchor alignment)
{
_contentText.text = LocalizeUtils.Get(TermCategory.Tutorial, text);
_contentText.color = color;
_contentText.alignment = alignment;
_contentText.fontSize = fontSize;
if (string.IsNullOrEmpty(text)) //If string is empty hide the textbox.
{
_textRF.gameObject.SetActive(false);
}
}
#endregion
#region Character Dialogue
public void ToggleCharacter(bool isShown, string text = "", string animNameToPlay = "")
{
if (_characterGO != null) {
_characterGO.SetActive(isShown);
if (isShown) {
_characterText.text = LocalizeUtils.Get(TermCategory.Tutorial, text);
if (_characterAnim != null) {
if (_characterAnim.GetClip(animNameToPlay))
TimeManager.Start(AnimationUtils.Play(_characterAnim, animNameToPlay, false));
else
_characterAnim.Play();
}
}
}
}
#endregion
public void Reset(bool hideBackgroundMask = true)
{
if (hideBackgroundMask)
ToggleBackgroundMask(false);
ToggleHand(false);
SetHandAnimation(_defaultHandAnimationClipName);
SetText(string.Empty);
}
public static void DestroyInstance()
{
if (_instance != null)
Destroy(_instance.gameObject);
}
private void OnDisable()
{
StopAllCoroutines();
}
}
}