-
Notifications
You must be signed in to change notification settings - Fork 1
/
CartoonEffects.cs
315 lines (297 loc) · 13.3 KB
/
CartoonEffects.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
// 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 UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Fungus
{
public enum cartoonType2
{
/// <summary> Fade In effect sequence. </summary>
Enable,
/// <summary> Stop All </summary>
Disable
}
/// <summary>
/// Tween sequence
/// </summary>
[CommandInfo("Animation",
"Cartoon Effects2",
"Sequence of tween animations. For Overlay mode/object to be Always-On-Top, drag and drop your Camera to inspector")]
[AddComponentMenu("")]
[ExecuteInEditMode]
public partial class CartoonEffects : Command
{
[Tooltip("Wait until the tween has finished before executing the next command")]
[HideInInspector] protected bool waitUntilFinished = true;
[Tooltip("Where the effect spawns on screen")]
[HideInInspector] public Vector3 effectPosition;
[Tooltip("Enable")]
[SerializeField] public cartoonType2 enable;
[Tooltip("GameObject/Bitmaps")]
[SerializeField] public Image mainSprite;
[Tooltip("The shape of the easing curve applied to the animation")]
[SerializeField] protected LeanTweenType mainEaseType = LeanTweenType.punch;
[Tooltip("Scales sprite")]
[SerializeField] public float mainSpriteScale = 1f;
[Tooltip("Disable sprite")]
[SerializeField] public bool disableSprite = false;
[Tooltip("The type of loop to apply once the animation has completed")]
[SerializeField]
protected LeanTweenType mainLoopType = LeanTweenType.clamp;
[Tooltip("Tween rotation")]
[SerializeField] public float rotationAngle = 30f;
[Tooltip("Tween rotation")]
[SerializeField] public bool rotationLoop = false;
[Tooltip("particles")]
[SerializeField] public Image secondarySprite;
[Tooltip("Scales secondary sprite size")]
[SerializeField] public float secondaryScale = 0.4f;
[Tooltip("Tween rotation")]
[SerializeField] public float secondaryRotation = 30f;
[Tooltip("Tween rotation")]
[SerializeField] public bool secondaryLoop = false;
[Tooltip("particles")]
[SerializeField] public Image particleSprite;
[Tooltip("The shape of the easing curve applied to the animation")]
[SerializeField] protected LeanTweenType pEaseType = LeanTweenType.easeInOutCubic;
[Tooltip("Tween x pattern")]
[SerializeField] public bool xPattern = false;
[Tooltip("Tween + pattern")]
[SerializeField] public bool plusPattern = false;
[Tooltip("Tween duration")]
[SerializeField] public float tweenDurations = 0.4f;
[Tooltip("Fade duration")]
[SerializeField] public float fadeDurations = 0.4f;
[Tooltip("Particle tween rotation")]
[SerializeField] public float rotateParticles = 0f;
[Tooltip("Scales particle size")]
[SerializeField] public float particleScale = 0.4f;
[Tooltip("Disable particle sprites")]
[SerializeField] public bool disableParticle = false;
[Tooltip("Enable continuous fading while tweening to particles")]
[SerializeField] public bool enableAlpha = false;
[Header("Location on where the effect appears on screen. The camera option is OPTIONAL!")]
[Tooltip("Where the effects should be appeared on screen, x position")]
[SerializeField] public float posX = 200f;
[Tooltip("Where the effects should be appeared on screen, y position")]
[SerializeField] public float posY = 200f;
[Space(6)]
[Tooltip("Sorting order")]
[SerializeField] public int sortingOrder = 0;
[Space(4)]
[Tooltip("Screen Space Camera mode, if empty Overlay mode is used")]
[SerializeField] public Camera optionalMainCamera;
[Tooltip("Store them in a List and make them re-usable")]
[SerializeField] public List<Image> partcList = new List<Image>();
private IEnumerator coroutine;
private Canvas nCanvas;
private int objIndexNum;
private int midObjIndexNum;
private int lastIndexNum;
int onLast = 0;
int onMid = 8;
int onTop = 16;
protected void saveWait()
{
coroutine = StopAnim(0.1f);
//Start all delayed calls
StartCoroutine(coroutine);
}
protected void crCanvas()
{
//Programmatically create an empty Canvas.
Canvas myCanvas;
CanvasScaler myCanvasScaler;
GraphicRaycaster myGraphicRaycaster;
GameObject myGO = new GameObject("stvphtwod");
myGO.AddComponent<Canvas>();
myCanvas = myGO.GetComponent<Canvas>();
if (optionalMainCamera != null)
{
myCanvas.renderMode = RenderMode.ScreenSpaceCamera;
myCanvas.worldCamera = optionalMainCamera;
}
else
{
myCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
}
myCanvas.pixelPerfect = true;
myCanvas.sortingOrder = sortingOrder;
myGO.AddComponent<CanvasScaler>();
myCanvasScaler = myGO.GetComponent<CanvasScaler>();
myCanvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
myCanvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
myCanvasScaler.matchWidthOrHeight = 1f;
myGO.AddComponent<GraphicRaycaster>();
myGraphicRaycaster = myGO.GetComponent<GraphicRaycaster>();
myGraphicRaycaster.blockingObjects = GraphicRaycaster.BlockingObjects.All;
nCanvas = myCanvas.GetComponent<Canvas>();
}
//Main method
protected void Splash(int partcNum)
{ //If Canvas exist, us that, otherwise generate new one
if (nCanvas == null)
{
crCanvas();
}
midObjIndexNum = onLast;
objIndexNum = onTop;
effectPosition = new Vector3(posX, posY, 1f);
var spunPoint = effectPosition;
//Initial pivot location for object spawn
if (!disableParticle)
{
particleSprite.enabled = true;
}
if (mainSprite != null && disableSprite == false)
{
Image blobmain = Instantiate(mainSprite, spunPoint, Quaternion.identity) as Image;
blobmain.name = "ssplashpopp-main";
blobmain.transform.SetParent(nCanvas.transform, false);
blobmain.transform.SetAsLastSibling();
blobmain.transform.localScale = new Vector3(mainSpriteScale, mainSpriteScale, 1f);
LeanTween.scale(blobmain.rectTransform, Vector3.zero, tweenDurations).setEase(mainEaseType).setLoopType(mainLoopType);
blobmain.enabled = true;
if (rotationLoop)
{
LeanTween.rotateAroundLocal(blobmain.rectTransform, Vector3.forward, rotationAngle, 0.3f).setLoopPingPong(-1);
}
}
if (particleSprite != null && disableParticle == false)
{
for (int i = 0; i < partcNum; i++)
{
Image blob0 = Instantiate(particleSprite, spunPoint, Quaternion.identity) as Image;
blob0.name = "ssplashpopp" + (i + 1);
blob0.transform.SetParent(nCanvas.transform, false);
blob0.transform.SetSiblingIndex(midObjIndexNum++);
blob0.transform.localScale = new Vector3(particleScale, particleScale, 1f);
partcList.Add(blob0);
if (disableParticle == true && particleSprite != null)
{
blob0.enabled = false;
}
if (disableParticle == false && particleSprite != null)
{
blob0.enabled = true;
}
}
//Set max particles & get from the List
int maxParticles = 7;
for (int w = maxParticles; w < partcList.Count; w++)
{
foreach (Image partcb in partcList)
{
LeanTween.scale(partcb.rectTransform, new Vector3(0f, 0f, 0f), tweenDurations).setEaseInQuad().setLoopClamp();
if (enableAlpha == true && disableParticle == false && particleSprite != null)
{
LeanTween.alpha(partcb.rectTransform, 0f, fadeDurations).setEase(LeanTweenType.linear).setLoopClamp();
}
if (rotateParticles > 0 && disableParticle == false && particleSprite != null)
{
LeanTween.rotateAroundLocal(partcb.rectTransform, Vector3.forward, rotationAngle, 0.3f).setLoopPingPong(-1);
}
}
if (plusPattern)
{
LeanTween.moveLocal(partcList[0].gameObject, spunPoint + new Vector3(160, 0f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[4].gameObject, spunPoint + new Vector3(0f, -180f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[5].gameObject, spunPoint + new Vector3(-160f, 0f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[2].gameObject, spunPoint + new Vector3(0f, 180f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
}
if (xPattern)
{
LeanTween.moveLocal(partcList[1].gameObject, spunPoint + new Vector3(130f, 120f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[3].gameObject, spunPoint + new Vector3(130f, -120f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[6].gameObject, spunPoint + new Vector3(-130f, -120f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
LeanTween.moveLocal(partcList[7].gameObject, spunPoint + new Vector3(-130f, 120f, 0f), tweenDurations).setLoopClamp().setEase(pEaseType);
}
}
}
if (waitUntilFinished)
{
Continue();
}
}
//Destroy all instantiated objects
protected IEnumerator StopAnim(float searchNdestroy)
{
if (partcList != null)
{
if (particleSprite != null)
{
particleSprite.enabled = false;
}
if (mainSprite != null)
{
particleSprite.enabled = false;
}
var imgclones = Resources.FindObjectsOfTypeAll<Image>();
foreach (var bg in imgclones)
{
if (bg.name.StartsWith("ssplashpopp"))
{
LeanTween.scale(bg.gameObject, new Vector3(-1f, 0f, 0f), 0.2f).setDestroyOnComplete(true);
partcList.Clear();
partcList.TrimExcess();
}
}
yield return new WaitForSeconds(searchNdestroy);
var canvcont = Resources.FindObjectsOfTypeAll<Canvas>().Where(obj => obj.name == "stvphtwod");
foreach (Canvas canvs in canvcont)
{
if (canvs != null)
{
GameObject.Destroy(canvs.gameObject);
}
}
Debug.Log("Item =" + partcList.Count);
Debug.Log("Element =" + partcList.Capacity);
if (waitUntilFinished)
{
Continue();
}
}
}
#region Public members
public override Color GetButtonColor()
{
return new Color32(221, 184, 169, 255);
}
public override void OnEnter()
{
Canvas.ForceUpdateCanvases();
//RunForestRun();
if (mainSprite != null && disableSprite == false)
{
mainSprite.enabled = false;
}
if (particleSprite != null)
{
particleSprite.enabled = false;
}
switch (enable)
{
case (cartoonType2.Enable):
Splash(8);
SecondaryEf(2);
break;
case (cartoonType2.Disable):
saveWait();
break;
}
if (!waitUntilFinished)
{
Continue();
}
}
public override void OnCommandAdded(Block parentBlock)
{
enable = cartoonType2.Disable;
}
#endregion
}
}