| | 1 | | // Author: Daniele Giardini - http://www.demigiant.com |
| | 2 | | // Created: 2018/07/13 |
| | 3 | |
|
| | 4 | | #if true && (UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER) // MODULE_MARKER |
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | | using DG.Tweening.Core; |
| | 8 | | using DG.Tweening.Plugins.Options; |
| | 9 | |
|
| | 10 | | #pragma warning disable 1591 |
| | 11 | | namespace DG.Tweening |
| | 12 | | { |
| | 13 | | public static class DOTweenModuleSprite |
| | 14 | | { |
| | 15 | | #region Shortcuts |
| | 16 | |
|
| | 17 | | #region SpriteRenderer |
| | 18 | |
|
| | 19 | | /// <summary>Tweens a SpriteRenderer's color to the given value. |
| | 20 | | /// Also stores the spriteRenderer as the tween's target so it can be used for filtered operations</summary> |
| | 21 | | /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param |
| | 22 | | public static TweenerCore<Color, Color, ColorOptions> DOColor(this SpriteRenderer target, Color endValue, float |
| | 23 | | { |
| 0 | 24 | | TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, |
| 0 | 25 | | t.SetTarget(target); |
| 0 | 26 | | return t; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary>Tweens a Material's alpha color to the given value. |
| | 30 | | /// Also stores the spriteRenderer as the tween's target so it can be used for filtered operations</summary> |
| | 31 | | /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param |
| | 32 | | public static TweenerCore<Color, Color, ColorOptions> DOFade(this SpriteRenderer target, float endValue, float d |
| | 33 | | { |
| 0 | 34 | | TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endVa |
| 0 | 35 | | t.SetTarget(target); |
| 0 | 36 | | return t; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary>Tweens a SpriteRenderer's color using the given gradient |
| | 40 | | /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener). |
| | 41 | | /// Also stores the image as the tween's target so it can be used for filtered operations</summary> |
| | 42 | | /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param> |
| | 43 | | public static Sequence DOGradientColor(this SpriteRenderer target, Gradient gradient, float duration) |
| | 44 | | { |
| 0 | 45 | | Sequence s = DOTween.Sequence(); |
| 0 | 46 | | GradientColorKey[] colors = gradient.colorKeys; |
| 0 | 47 | | int len = colors.Length; |
| 0 | 48 | | for (int i = 0; i < len; ++i) { |
| 0 | 49 | | GradientColorKey c = colors[i]; |
| 0 | 50 | | if (i == 0 && c.time <= 0) { |
| 0 | 51 | | target.color = c.color; |
| 0 | 52 | | continue; |
| | 53 | | } |
| 0 | 54 | | float colorDuration = i == len - 1 |
| | 55 | | ? duration - s.Duration(false) // Verifies that total duration is correct |
| | 56 | | : duration * (i == 0 ? c.time : c.time - colors[i - 1].time); |
| 0 | 57 | | s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear)); |
| | 58 | | } |
| 0 | 59 | | s.SetTarget(target); |
| 0 | 60 | | return s; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | #endregion |
| | 64 | |
|
| | 65 | | #region Blendables |
| | 66 | |
|
| | 67 | | #region SpriteRenderer |
| | 68 | |
|
| | 69 | | /// <summary>Tweens a SpriteRenderer's color to the given value, |
| | 70 | | /// in a way that allows other DOBlendableColor tweens to work together on the same target, |
| | 71 | | /// instead than fight each other as multiple DOColor would do. |
| | 72 | | /// Also stores the SpriteRenderer as the tween's target so it can be used for filtered operations</summary> |
| | 73 | | /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param> |
| | 74 | | public static Tweener DOBlendableColor(this SpriteRenderer target, Color endValue, float duration) |
| | 75 | | { |
| 0 | 76 | | endValue = endValue - target.color; |
| 0 | 77 | | Color to = new Color(0, 0, 0, 0); |
| 0 | 78 | | return DOTween.To(() => to, x => { |
| 0 | 79 | | Color diff = x - to; |
| 0 | 80 | | to = x; |
| 0 | 81 | | target.color += diff; |
| 0 | 82 | | }, endValue, duration) |
| | 83 | | .Blendable().SetTarget(target); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | #endregion |
| | 87 | |
|
| | 88 | | #endregion |
| | 89 | |
|
| | 90 | | #endregion |
| | 91 | | } |
| | 92 | | } |
| | 93 | | #endif |