DOTweenModuleSprite.cs 4.1 KB

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