-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoveToViewUI.cs
380 lines (336 loc) · 13.8 KB
/
MoveToViewUI.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
// This code is part of the Fungus library (/~https://github.com/snozbot/fungus)
// It is released for free under the MIT open source license (/~https://github.com/snozbot/fungus/blob/master/LICENSE)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public enum scaleCharLeanTweenType
{
/// <summary> Fade In effect sequence. </summary>
SetCharacterScaleToDefault,
None
}
namespace Fungus
{
/// <summary>
/// Moves the camera to a location specified by a View object.
/// </summary>
[CommandInfo("Camera",
"Move To View UI",
"Moves the camera to a location specified by a View object.")]
[AddComponentMenu("")]
public class MoveToViewUI : Command
{
[Tooltip("Time for move effect to complete")]
[SerializeField] protected float duration = 1;
[Tooltip("View to transition to when move is complete")]
[SerializeField] protected View targetView;
public virtual View TargetView { get { return targetView; } }
[Tooltip("Wait until the fade has finished before executing next command")]
[SerializeField] protected bool waitUntilFinished = true;
[Tooltip("Camera to use for the pan. Will use main camera if set to none.")]
[SerializeField] protected Camera targetCamera;
[SerializeField] protected LeanTweenType orthoSizeTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType posTweenType = LeanTweenType.easeInOutQuad;
[SerializeField] protected LeanTweenType rotTweenType = LeanTweenType.easeInOutQuad;
[Tooltip("Optional Canvas setup, if the canvas is ScreenSpace")]
[SerializeField] protected Canvas canvas;
[Tooltip("This may cause fickers on Enable/Disable during runtime. This change your canvas to World Space permanently")]
[SerializeField] protected bool forceToCanvasWorldSpace = false;
[Tooltip("Screen Resolution")]
[SerializeField] protected Vector2 screenResolution = new Vector2(1920, 1080);
[SerializeField] scaleCharLeanTweenType status = scaleCharLeanTweenType.None;
[Tooltip("Character to zoom & scale")]
[SerializeField] protected Character character;
[Tooltip("Scale character")]
[SerializeField] protected Vector3 scaleCharacterUI = new Vector3(1f, 1f, 1f);
[Tooltip("Enable scale")]
[SerializeField] protected bool enableScale = true;
[Tooltip("Enable move")]
[SerializeField] protected bool enableMove = false;
[Tooltip("Move character")]
[SerializeField] protected Vector3 moveCharacterUI = new Vector3(0f, 0f, 0f);
[Tooltip("Duration")]
[SerializeField] protected float scaleMoveDuration = 0.5f;
[Tooltip("Ease type for the scale tween.")]
[SerializeField] protected LeanTweenType easeType;
private static List<GameObject> cacheChar = new List<GameObject>();
private static bool canvasIsWorldSpace = false;
protected bool isCompleted = false; protected bool moveIsCompleted = false; protected bool moveIsCompleted1 = false;
protected bool moveIsCompleted2 = false; protected bool moveIsCompleted3 = false;
protected virtual void AcquireCamera()
{
if (targetCamera != null)
{
return;
}
targetCamera = Camera.main;
if (targetCamera == null)
{
targetCamera = GameObject.FindObjectOfType<Camera>();
}
}
protected virtual void SetDefaultCharScale()
{
if (character != null)
{
for (int i = 0; i < cacheChar.Count; i++)
{
Canvas.ForceUpdateCanvases();
var b = cacheChar[i];
var c = b.GetComponent<RectTransform>();
LeanTween.scale(c, Vector3.one, 0.5f).setRecursive(true).setEase(easeType).setOnComplete(() =>
{
c.anchoredPosition = Vector3.zero;
isCompleted = false;
//SetToDefaultCanvasSpace();
});
}
}
}
protected virtual void SetDefaultCharPosition()
{
if (character != null)
{
Canvas.ForceUpdateCanvases();
for (int i = 0; i < cacheChar.Count; i++)
{
var b = cacheChar[i];
var c = b.GetComponent<RectTransform>();
float newOffsetMinnX = c.offsetMin.x;
float newOffsetMinnY = c.offsetMin.y;
float newOffsetMaxxX = c.offsetMax.x;
float newOffsetMaxxY = c.offsetMax.y;
LeanTween.value(b, newOffsetMinnX, 0f, 0.5f).setOnUpdate((float val) =>
{
c.offsetMin = new Vector3(val, c.offsetMin.y, 0f);
}).setOnComplete(() =>
{
c.offsetMin = new Vector2(0, 0);
moveIsCompleted = false;
});
LeanTween.value(b, newOffsetMinnY, 0f, 0.5f).setOnUpdate((float val) =>
{
c.offsetMin = new Vector3(c.offsetMin.x, val, 0f);
}).setOnComplete(() =>
{
c.offsetMin = new Vector2(0, 0);
moveIsCompleted1 = false;
});
LeanTween.value(b, newOffsetMaxxX, 0f, 0.5f).setOnUpdate((float val) =>
{
c.offsetMax = new Vector3(val, c.offsetMax.y, 0f);
}).setOnComplete(() =>
{
c.offsetMax = new Vector2(0, 0);
moveIsCompleted2 = false;
});
LeanTween.value(b, newOffsetMaxxY, 0f, 0.5f).setOnUpdate((float val) =>
{
c.offsetMax = new Vector3(c.offsetMax.x, val, 0f);
}).setOnComplete(() =>
{
c.offsetMax = new Vector2(0, 0);
moveIsCompleted3 = false;
});
StartCoroutine(FinalStop());
}
}
}
protected virtual IEnumerator FinalStop()
{
while(true)
{
yield return null;
if(!moveIsCompleted && !moveIsCompleted1 && !moveIsCompleted2 && !moveIsCompleted3 && !isCompleted)
{
break;
}
}
StopsAll();
}
protected virtual void StopsAll()
{
for (int i = character.State.holder.transform.childCount - 1; i >= 0; i--)
{
var b = character.State.holder.transform.GetChild(i).gameObject;
b.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
}
cacheChar = new List<GameObject>();
StopAllCoroutines();
}
protected virtual void ScaleMoveCharacter()
{
if (character && character.State.portraitImage != null)
{
if(cacheChar != null)
{
var holdersS = character.State.holder.GetComponent<RectTransform>();
if(forceToCanvasWorldSpace)
{
SetCanvasToWorldSpace();
}
if (enableScale)
{
for (int i = 0; i < cacheChar.Count; i++)
{
var b = cacheChar[i];
var c = b.GetComponent<RectTransform>();
LeanTween.scale(c, scaleCharacterUI, scaleMoveDuration).setRecursive(true).setEase(easeType).setOnComplete(() =>
{
isCompleted = true;
});
}
}
if (enableMove)
{
for (int i = 0; i < cacheChar.Count; i++)
{
var b = cacheChar[i];
var c = b.GetComponent<RectTransform>();
LeanTween.move(c, moveCharacterUI, scaleMoveDuration).setEase(easeType).setOnComplete(() =>
{
moveIsCompleted = true;
moveIsCompleted1 = true;
moveIsCompleted2 = true;
moveIsCompleted3 = true;
});
}
}
}
}
}
protected virtual IEnumerator SetToDefaultCanvasSpace()
{
if (canvas != null)
{
if (targetCamera != null)
{
yield return new WaitForSeconds(1f);
Canvas.ForceUpdateCanvases();
canvas.renderMode = RenderMode.ScreenSpaceCamera;
canvas.worldCamera = targetCamera;
var canvy = canvas.GetComponent<CanvasScaler>();
canvy.referenceResolution = screenResolution;
canvy.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvy.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
canvasIsWorldSpace = false;
}
}
}
protected void SetCanvasToWorldSpace()
{
Canvas.ForceUpdateCanvases();
if (canvas != null)
{
if (targetCamera != null)
{
canvasIsWorldSpace = false;
canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = targetCamera;
}
}
}
public virtual void Start()
{
AcquireCamera();
}
#region Public members
public override void OnEnter()
{
if (status == scaleCharLeanTweenType.SetCharacterScaleToDefault)
{
moveIsCompleted = true;
moveIsCompleted1 = true;
moveIsCompleted2 = true;
moveIsCompleted3 = true;
isCompleted = true;
SetDefaultCharPosition();
SetDefaultCharScale();
AcquireCamera();
if (targetCamera == null ||
targetView == null)
{
Continue();
return;
}
var cameraManager = FungusManager.Instance.CameraManager;
Vector3 targetPosition = targetView.transform.position;
Quaternion targetRotation = targetView.transform.rotation;
float targetSize = targetView.ViewSize;
cameraManager.PanToPosition(targetCamera, targetPosition, targetRotation, targetSize, duration, delegate
{
if (waitUntilFinished)
{
Continue();
}
}, orthoSizeTweenType, posTweenType, rotTweenType);
if (!waitUntilFinished)
{
Continue();
}
}
else
{
//Create new list on first run
cacheChar = new List<GameObject>();
//Cache the character to a list
if (character != null)
{
for (int i = character.State.holder.transform.childCount - 1; i >= 0; i--)
{
var b = character.State.holder.transform.GetChild(i).gameObject;
cacheChar.Add(b);
}
}
if (character != null)
{
ScaleMoveCharacter();
}
AcquireCamera();
if (targetCamera == null ||
targetView == null)
{
Continue();
return;
}
var cameraManager = FungusManager.Instance.CameraManager;
Vector3 targetPosition = targetView.transform.position;
Quaternion targetRotation = targetView.transform.rotation;
float targetSize = targetView.ViewSize;
cameraManager.PanToPosition(targetCamera, targetPosition, targetRotation, targetSize, duration, delegate
{
if (waitUntilFinished)
{
Continue();
}
}, orthoSizeTweenType, posTweenType, rotTweenType);
if (!waitUntilFinished)
{
Continue();
}
}
}
public override void OnStopExecuting()
{
var cameraManager = FungusManager.Instance.CameraManager;
cameraManager.Stop();
}
public override string GetSummary()
{
if (targetView == null)
{
return "Error: No view selected";
}
else
{
return targetView.name;
}
}
public override Color GetButtonColor()
{
return new Color32(216, 228, 170, 255);
}
#endregion
}
}