| | 1 | | // Author: Daniele Giardini - http://www.demigiant.com |
| | 2 | | // Created: 2018/07/13 |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using UnityEngine; |
| | 6 | | using DG.Tweening.Core; |
| | 7 | | using DG.Tweening.Plugins.Options; |
| | 8 | | //#if UNITY_2018_1_OR_NEWER && (NET_4_6 || NET_STANDARD_2_0) |
| | 9 | | //using Task = System.Threading.Tasks.Task; |
| | 10 | | //#endif |
| | 11 | |
|
| | 12 | | #pragma warning disable 1591 |
| | 13 | | namespace DG.Tweening |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Shortcuts/functions that are not strictly related to specific Modules |
| | 17 | | /// but are available only on some Unity versions |
| | 18 | | /// </summary> |
| | 19 | | public static class DOTweenModuleUnityVersion |
| | 20 | | { |
| | 21 | | #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER |
| | 22 | | #region Unity 4.3 or Newer |
| | 23 | |
|
| | 24 | | #region Material |
| | 25 | |
|
| | 26 | | /// <summary>Tweens a Material's color using the given gradient |
| | 27 | | /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener). |
| | 28 | | /// Also stores the image as the tween's target so it can be used for filtered operations</summary> |
| | 29 | | /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param> |
| | 30 | | public static Sequence DOGradientColor(this Material target, Gradient gradient, float duration) |
| | 31 | | { |
| | 32 | | Sequence s = DOTween.Sequence(); |
| | 33 | | GradientColorKey[] colors = gradient.colorKeys; |
| | 34 | | int len = colors.Length; |
| | 35 | | for (int i = 0; i < len; ++i) { |
| | 36 | | GradientColorKey c = colors[i]; |
| | 37 | | if (i == 0 && c.time <= 0) { |
| | 38 | | target.color = c.color; |
| | 39 | | continue; |
| | 40 | | } |
| | 41 | | float colorDuration = i == len - 1 |
| | 42 | | ? duration - s.Duration(false) // Verifies that total duration is correct |
| | 43 | | : duration * (i == 0 ? c.time : c.time - colors[i - 1].time); |
| | 44 | | s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear)); |
| | 45 | | } |
| | 46 | | s.SetTarget(target); |
| | 47 | | return s; |
| | 48 | | } |
| | 49 | | /// <summary>Tweens a Material's named color property using the given gradient |
| | 50 | | /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener). |
| | 51 | | /// Also stores the image as the tween's target so it can be used for filtered operations</summary> |
| | 52 | | /// <param name="gradient">The gradient to use</param> |
| | 53 | | /// <param name="property">The name of the material property to tween (like _Tint or _SpecColor)</param> |
| | 54 | | /// <param name="duration">The duration of the tween</param> |
| | 55 | | public static Sequence DOGradientColor(this Material target, Gradient gradient, string property, float duration) |
| | 56 | | { |
| | 57 | | Sequence s = DOTween.Sequence(); |
| | 58 | | GradientColorKey[] colors = gradient.colorKeys; |
| | 59 | | int len = colors.Length; |
| | 60 | | for (int i = 0; i < len; ++i) { |
| | 61 | | GradientColorKey c = colors[i]; |
| | 62 | | if (i == 0 && c.time <= 0) { |
| | 63 | | target.SetColor(property, c.color); |
| | 64 | | continue; |
| | 65 | | } |
| | 66 | | float colorDuration = i == len - 1 |
| | 67 | | ? duration - s.Duration(false) // Verifies that total duration is correct |
| | 68 | | : duration * (i == 0 ? c.time : c.time - colors[i - 1].time); |
| | 69 | | s.Append(target.DOColor(c.color, property, colorDuration).SetEase(Ease.Linear)); |
| | 70 | | } |
| | 71 | | s.SetTarget(target); |
| | 72 | | return s; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | #endregion |
| | 76 | |
|
| | 77 | | #endregion |
| | 78 | | #endif |
| | 79 | |
|
| | 80 | | #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER |
| | 81 | | #region Unity 5.3 or Newer |
| | 82 | |
|
| | 83 | | #region CustomYieldInstructions |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or complete. |
| | 87 | | /// It can be used inside a coroutine as a yield. |
| | 88 | | /// <para>Example usage:</para><code>yield return myTween.WaitForCompletion(true);</code> |
| | 89 | | /// </summary> |
| | 90 | | public static CustomYieldInstruction WaitForCompletion(this Tween t, bool returnCustomYieldInstruction) |
| | 91 | | { |
| | 92 | | if (!t.active) { |
| | 93 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 94 | | return null; |
| | 95 | | } |
| | 96 | | return new DOTweenCYInstruction.WaitForCompletion(t); |
| | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or rewinded. |
| | 101 | | /// It can be used inside a coroutine as a yield. |
| | 102 | | /// <para>Example usage:</para><code>yield return myTween.WaitForRewind();</code> |
| | 103 | | /// </summary> |
| | 104 | | public static CustomYieldInstruction WaitForRewind(this Tween t, bool returnCustomYieldInstruction) |
| | 105 | | { |
| | 106 | | if (!t.active) { |
| | 107 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 108 | | return null; |
| | 109 | | } |
| | 110 | | return new DOTweenCYInstruction.WaitForRewind(t); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed. |
| | 115 | | /// It can be used inside a coroutine as a yield. |
| | 116 | | /// <para>Example usage:</para><code>yield return myTween.WaitForKill();</code> |
| | 117 | | /// </summary> |
| | 118 | | public static CustomYieldInstruction WaitForKill(this Tween t, bool returnCustomYieldInstruction) |
| | 119 | | { |
| | 120 | | if (!t.active) { |
| | 121 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 122 | | return null; |
| | 123 | | } |
| | 124 | | return new DOTweenCYInstruction.WaitForKill(t); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or has gone through the |
| | 129 | | /// It can be used inside a coroutine as a yield. |
| | 130 | | /// <para>Example usage:</para><code>yield return myTween.WaitForElapsedLoops(2);</code> |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="elapsedLoops">Elapsed loops to wait for</param> |
| | 133 | | public static CustomYieldInstruction WaitForElapsedLoops(this Tween t, int elapsedLoops, bool returnCustomYieldI |
| | 134 | | { |
| | 135 | | if (!t.active) { |
| | 136 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 137 | | return null; |
| | 138 | | } |
| | 139 | | return new DOTweenCYInstruction.WaitForElapsedLoops(t, elapsedLoops); |
| | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed |
| | 144 | | /// or has reached the given time position (loops included, delays excluded). |
| | 145 | | /// It can be used inside a coroutine as a yield. |
| | 146 | | /// <para>Example usage:</para><code>yield return myTween.WaitForPosition(2.5f);</code> |
| | 147 | | /// </summary> |
| | 148 | | /// <param name="position">Position (loops included, delays excluded) to wait for</param> |
| | 149 | | public static CustomYieldInstruction WaitForPosition(this Tween t, float position, bool returnCustomYieldInstruc |
| | 150 | | { |
| | 151 | | if (!t.active) { |
| | 152 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 153 | | return null; |
| | 154 | | } |
| | 155 | | return new DOTweenCYInstruction.WaitForPosition(t, position); |
| | 156 | | } |
| | 157 | |
|
| | 158 | | /// <summary> |
| | 159 | | /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or started |
| | 160 | | /// (meaning when the tween is set in a playing state the first time, after any eventual delay). |
| | 161 | | /// It can be used inside a coroutine as a yield. |
| | 162 | | /// <para>Example usage:</para><code>yield return myTween.WaitForStart();</code> |
| | 163 | | /// </summary> |
| | 164 | | public static CustomYieldInstruction WaitForStart(this Tween t, bool returnCustomYieldInstruction) |
| | 165 | | { |
| | 166 | | if (!t.active) { |
| | 167 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 168 | | return null; |
| | 169 | | } |
| | 170 | | return new DOTweenCYInstruction.WaitForStart(t); |
| | 171 | | } |
| | 172 | |
|
| | 173 | | #endregion |
| | 174 | |
|
| | 175 | | #endregion |
| | 176 | | #endif |
| | 177 | |
|
| | 178 | | #if UNITY_2018_1_OR_NEWER |
| | 179 | | #region Unity 2018.1 or Newer |
| | 180 | |
|
| | 181 | | #region Material |
| | 182 | |
|
| | 183 | | /// <summary>Tweens a Material's named texture offset property with the given ID to the given value. |
| | 184 | | /// Also stores the material as the tween's target so it can be used for filtered operations</summary> |
| | 185 | | /// <param name="endValue">The end value to reach</param> |
| | 186 | | /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</pa |
| | 187 | | /// <param name="duration">The duration of the tween</param> |
| | 188 | | public static TweenerCore<Vector2, Vector2, VectorOptions> DOOffset(this Material target, Vector2 endValue, int |
| | 189 | | { |
| | 190 | | if (!target.HasProperty(propertyID)) { |
| | 191 | | if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID); |
| | 192 | | return null; |
| | 193 | | } |
| | 194 | | TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureOffset(propertyID), x => |
| | 195 | | t.SetTarget(target); |
| | 196 | | return t; |
| | 197 | | } |
| | 198 | |
|
| | 199 | | /// <summary>Tweens a Material's named texture scale property with the given ID to the given value. |
| | 200 | | /// Also stores the material as the tween's target so it can be used for filtered operations</summary> |
| | 201 | | /// <param name="endValue">The end value to reach</param> |
| | 202 | | /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</pa |
| | 203 | | /// <param name="duration">The duration of the tween</param> |
| | 204 | | public static TweenerCore<Vector2, Vector2, VectorOptions> DOTiling(this Material target, Vector2 endValue, int |
| | 205 | | { |
| | 206 | | if (!target.HasProperty(propertyID)) { |
| | 207 | | if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID); |
| | 208 | | return null; |
| | 209 | | } |
| | 210 | | TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureScale(propertyID), x => t |
| | 211 | | t.SetTarget(target); |
| | 212 | | return t; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | #endregion |
| | 216 | |
|
| | 217 | | #region .NET 4.6 or Newer |
| | 218 | |
|
| | 219 | | #if UNITY_2018_1_OR_NEWER && (NET_4_6 || NET_STANDARD_2_0) |
| | 220 | |
|
| | 221 | | #region Async Instructions |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or complete. |
| | 225 | | /// It can be used inside an async operation. |
| | 226 | | /// <para>Example usage:</para><code>await myTween.WaitForCompletion();</code> |
| | 227 | | /// </summary> |
| | 228 | | public static async System.Threading.Tasks.Task AsyncWaitForCompletion(this Tween t) |
| | 229 | | { |
| | 230 | | if (!t.active) { |
| | 231 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 232 | | return; |
| | 233 | | } |
| | 234 | | while (t.active && !t.IsComplete()) await System.Threading.Tasks.Task.Yield(); |
| | 235 | | } |
| | 236 | |
|
| | 237 | | /// <summary> |
| | 238 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or rewinded. |
| | 239 | | /// It can be used inside an async operation. |
| | 240 | | /// <para>Example usage:</para><code>await myTween.AsyncWaitForRewind();</code> |
| | 241 | | /// </summary> |
| | 242 | | public static async System.Threading.Tasks.Task AsyncWaitForRewind(this Tween t) |
| | 243 | | { |
| | 244 | | if (!t.active) { |
| | 245 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 246 | | return; |
| | 247 | | } |
| | 248 | | while (t.active && (!t.playedOnce || t.position * (t.CompletedLoops() + 1) > 0)) await System.Threading.Task |
| | 249 | | } |
| | 250 | |
|
| | 251 | | /// <summary> |
| | 252 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed. |
| | 253 | | /// It can be used inside an async operation. |
| | 254 | | /// <para>Example usage:</para><code>await myTween.AsyncWaitForKill();</code> |
| | 255 | | /// </summary> |
| | 256 | | public static async System.Threading.Tasks.Task AsyncWaitForKill(this Tween t) |
| | 257 | | { |
| | 258 | | if (!t.active) { |
| | 259 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 260 | | return; |
| | 261 | | } |
| | 262 | | while (t.active) await System.Threading.Tasks.Task.Yield(); |
| | 263 | | } |
| | 264 | |
|
| | 265 | | /// <summary> |
| | 266 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or has gone |
| | 267 | | /// It can be used inside an async operation. |
| | 268 | | /// <para>Example usage:</para><code>await myTween.AsyncWaitForElapsedLoops();</code> |
| | 269 | | /// </summary> |
| | 270 | | /// <param name="elapsedLoops">Elapsed loops to wait for</param> |
| | 271 | | public static async System.Threading.Tasks.Task AsyncWaitForElapsedLoops(this Tween t, int elapsedLoops) |
| | 272 | | { |
| | 273 | | if (!t.active) { |
| | 274 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 275 | | return; |
| | 276 | | } |
| | 277 | | while (t.active && t.CompletedLoops() < elapsedLoops) await System.Threading.Tasks.Task.Yield(); |
| | 278 | | } |
| | 279 | |
|
| | 280 | | /// <summary> |
| | 281 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or started |
| | 282 | | /// (meaning when the tween is set in a playing state the first time, after any eventual delay). |
| | 283 | | /// It can be used inside an async operation. |
| | 284 | | /// <para>Example usage:</para><code>await myTween.AsyncWaitForPosition();</code> |
| | 285 | | /// </summary> |
| | 286 | | /// <param name="position">Position (loops included, delays excluded) to wait for</param> |
| | 287 | | public static async System.Threading.Tasks.Task AsyncWaitForPosition(this Tween t, float position) |
| | 288 | | { |
| | 289 | | if (!t.active) { |
| | 290 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 291 | | return; |
| | 292 | | } |
| | 293 | | while (t.active && t.position * (t.CompletedLoops() + 1) < position) await System.Threading.Tasks.Task.Yield |
| | 294 | | } |
| | 295 | |
|
| | 296 | | /// <summary> |
| | 297 | | /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed. |
| | 298 | | /// It can be used inside an async operation. |
| | 299 | | /// <para>Example usage:</para><code>await myTween.AsyncWaitForKill();</code> |
| | 300 | | /// </summary> |
| | 301 | | public static async System.Threading.Tasks.Task AsyncWaitForStart(this Tween t) |
| | 302 | | { |
| | 303 | | if (!t.active) { |
| | 304 | | if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t); |
| | 305 | | return; |
| | 306 | | } |
| | 307 | | while (t.active && !t.playedOnce) await System.Threading.Tasks.Task.Yield(); |
| | 308 | | } |
| | 309 | |
|
| | 310 | | #endregion |
| | 311 | | #endif |
| | 312 | |
|
| | 313 | | #endregion |
| | 314 | |
|
| | 315 | | #endregion |
| | 316 | | #endif |
| | 317 | | } |
| | 318 | |
|
| | 319 | | // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████ |
| | 320 | | // ███ CLASSES █████████████████████████████████████████████████████████████████████████████████████████████████████ |
| | 321 | | // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████ |
| | 322 | |
|
| | 323 | | #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER |
| | 324 | | public static class DOTweenCYInstruction |
| | 325 | | { |
| | 326 | | public class WaitForCompletion : CustomYieldInstruction |
| | 327 | | { |
| | 328 | | public override bool keepWaiting { get { |
| 0 | 329 | | return t.active && !t.IsComplete(); |
| | 330 | | }} |
| | 331 | | readonly Tween t; |
| 0 | 332 | | public WaitForCompletion(Tween tween) |
| | 333 | | { |
| 0 | 334 | | t = tween; |
| 0 | 335 | | } |
| | 336 | | } |
| | 337 | |
|
| | 338 | | public class WaitForRewind : CustomYieldInstruction |
| | 339 | | { |
| | 340 | | public override bool keepWaiting { get { |
| 0 | 341 | | return t.active && (!t.playedOnce || t.position * (t.CompletedLoops() + 1) > 0); |
| | 342 | | }} |
| | 343 | | readonly Tween t; |
| 0 | 344 | | public WaitForRewind(Tween tween) |
| | 345 | | { |
| 0 | 346 | | t = tween; |
| 0 | 347 | | } |
| | 348 | | } |
| | 349 | |
|
| | 350 | | public class WaitForKill : CustomYieldInstruction |
| | 351 | | { |
| | 352 | | public override bool keepWaiting { get { |
| 0 | 353 | | return t.active; |
| | 354 | | }} |
| | 355 | | readonly Tween t; |
| 0 | 356 | | public WaitForKill(Tween tween) |
| | 357 | | { |
| 0 | 358 | | t = tween; |
| 0 | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| | 362 | | public class WaitForElapsedLoops : CustomYieldInstruction |
| | 363 | | { |
| | 364 | | public override bool keepWaiting { get { |
| 0 | 365 | | return t.active && t.CompletedLoops() < elapsedLoops; |
| | 366 | | }} |
| | 367 | | readonly Tween t; |
| | 368 | | readonly int elapsedLoops; |
| 0 | 369 | | public WaitForElapsedLoops(Tween tween, int elapsedLoops) |
| | 370 | | { |
| 0 | 371 | | t = tween; |
| 0 | 372 | | this.elapsedLoops = elapsedLoops; |
| 0 | 373 | | } |
| | 374 | | } |
| | 375 | |
|
| | 376 | | public class WaitForPosition : CustomYieldInstruction |
| | 377 | | { |
| | 378 | | public override bool keepWaiting { get { |
| 0 | 379 | | return t.active && t.position * (t.CompletedLoops() + 1) < position; |
| | 380 | | }} |
| | 381 | | readonly Tween t; |
| | 382 | | readonly float position; |
| 0 | 383 | | public WaitForPosition(Tween tween, float position) |
| | 384 | | { |
| 0 | 385 | | t = tween; |
| 0 | 386 | | this.position = position; |
| 0 | 387 | | } |
| | 388 | | } |
| | 389 | |
|
| | 390 | | public class WaitForStart : CustomYieldInstruction |
| | 391 | | { |
| | 392 | | public override bool keepWaiting { get { |
| 0 | 393 | | return t.active && !t.playedOnce; |
| | 394 | | }} |
| | 395 | | readonly Tween t; |
| 0 | 396 | | public WaitForStart(Tween tween) |
| | 397 | | { |
| 0 | 398 | | t = tween; |
| 0 | 399 | | } |
| | 400 | | } |
| | 401 | | } |
| | 402 | | #endif |
| | 403 | | } |