DOTweenTk2d.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2014/10/27 15:59
  3. //
  4. // License Copyright (c) Daniele Giardini.
  5. // This work is subject to the terms at http://dotween.demigiant.com/license.php
  6. #if false // MODULE_MARKER
  7. using DG.Tweening.Core;
  8. using DG.Tweening.Plugins.Options;
  9. using UnityEngine;
  10. namespace DG.Tweening
  11. {
  12. /// <summary>
  13. /// Methods that extend 2D Toolkit objects and allow to directly create and control tweens from their instances.
  14. /// </summary>
  15. public static class ShortcutExtensionsTk2d
  16. {
  17. #region Sprite
  18. /// <summary>Tweens a 2D Toolkit Sprite's dimensions to the given value.
  19. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  20. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  21. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScale(this tk2dBaseSprite target, Vector3 endValue, float duration)
  22. {
  23. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, endValue, duration);
  24. t.SetTarget(target);
  25. return t;
  26. }
  27. /// <summary>Tweens a Sprite's dimensions to the given value.
  28. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  29. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  30. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleX(this tk2dBaseSprite target, float endValue, float duration)
  31. {
  32. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(endValue, 0, 0), duration);
  33. t.SetOptions(AxisConstraint.X)
  34. .SetTarget(target);
  35. return t;
  36. }
  37. /// <summary>Tweens a Sprite's dimensions to the given value.
  38. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  39. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  40. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleY(this tk2dBaseSprite target, float endValue, float duration)
  41. {
  42. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(0, endValue, 0), duration);
  43. t.SetOptions(AxisConstraint.Y)
  44. .SetTarget(target);
  45. return t;
  46. }
  47. /// <summary>Tweens a Sprite's dimensions to the given value.
  48. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  49. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  50. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleZ(this tk2dBaseSprite target, float endValue, float duration)
  51. {
  52. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(0, 0, endValue), duration);
  53. t.SetOptions(AxisConstraint.Z)
  54. .SetTarget(target);
  55. return t;
  56. }
  57. /// <summary>Tweens a 2D Toolkit Sprite's color to the given value.
  58. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  59. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  60. public static TweenerCore<Color, Color, ColorOptions> DOColor(this tk2dBaseSprite target, Color endValue, float duration)
  61. {
  62. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  63. t.SetTarget(target);
  64. return t;
  65. }
  66. /// <summary>Tweens a 2D Toolkit Sprite's alpha color to the given value.
  67. /// Also stores the Sprite as the tween's target so it can be used for filtered operations</summary>
  68. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  69. public static TweenerCore<Color, Color, ColorOptions> DOFade(this tk2dBaseSprite target, float endValue, float duration)
  70. {
  71. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  72. t.SetTarget(target);
  73. return t;
  74. }
  75. /// <summary>Tweens a 2D Toolkit Sprite's color using the given gradient
  76. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  77. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  78. /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
  79. public static Sequence DOGradientColor(this tk2dBaseSprite target, Gradient gradient, float duration)
  80. {
  81. Sequence s = DOTween.Sequence();
  82. GradientColorKey[] colors = gradient.colorKeys;
  83. int len = colors.Length;
  84. for (int i = 0; i < len; ++i) {
  85. GradientColorKey c = colors[i];
  86. if (i == 0 && c.time <= 0) {
  87. target.color = c.color;
  88. continue;
  89. }
  90. float colorDuration = i == len - 1
  91. ? duration - s.Duration(false) // Verifies that total duration is correct
  92. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  93. s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
  94. }
  95. s.SetTarget(target);
  96. return s;
  97. }
  98. #endregion
  99. #region tk2dSlicedSprite
  100. /// <summary>Tweens a 2D Toolkit SlicedSprite's dimensions to the given value.
  101. /// Also stores the SlicedSprite as the tween's target so it can be used for filtered operations</summary>
  102. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  103. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScaleDimensions(this tk2dSlicedSprite target, Vector2 endValue, float duration)
  104. {
  105. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.dimensions, x => target.dimensions = x, endValue, duration);
  106. t.SetTarget(target);
  107. return t;
  108. }
  109. /// <summary>Tweens a SlicedSprite's dimensions to the given value.
  110. /// Also stores the SlicedSprite as the tween's target so it can be used for filtered operations</summary>
  111. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  112. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScaleDimensionsX(this tk2dSlicedSprite target, float endValue, float duration)
  113. {
  114. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.dimensions, x => target.dimensions = x, new Vector2(endValue, 0), duration);
  115. t.SetOptions(AxisConstraint.X)
  116. .SetTarget(target);
  117. return t;
  118. }
  119. /// <summary>Tweens a SlicedSprite's dimensions to the given value.
  120. /// Also stores the SlicedSprite as the tween's target so it can be used for filtered operations</summary>
  121. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  122. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScaleDimensionsY(this tk2dSlicedSprite target, float endValue, float duration)
  123. {
  124. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.dimensions, x => target.dimensions = x, new Vector2(0, endValue), duration);
  125. t.SetOptions(AxisConstraint.Y)
  126. .SetTarget(target);
  127. return t;
  128. }
  129. #endregion
  130. #region TextMesh
  131. /// <summary>Tweens a 2D Toolkit TextMesh's dimensions to the given value.
  132. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  133. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  134. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScale(this tk2dTextMesh target, Vector3 endValue, float duration)
  135. {
  136. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, endValue, duration);
  137. t.SetTarget(target);
  138. return t;
  139. }
  140. /// <summary>Tweens a 2D Toolkit TextMesh's dimensions to the given value.
  141. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  142. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  143. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleX(this tk2dTextMesh target, float endValue, float duration)
  144. {
  145. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(endValue, 0, 0), duration);
  146. t.SetOptions(AxisConstraint.X)
  147. .SetTarget(target);
  148. return t;
  149. }
  150. /// <summary>Tweens a 2D Toolkit TextMesh's dimensions to the given value.
  151. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  152. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  153. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleY(this tk2dTextMesh target, float endValue, float duration)
  154. {
  155. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(0, endValue, 0), duration);
  156. t.SetOptions(AxisConstraint.Y)
  157. .SetTarget(target);
  158. return t;
  159. }
  160. /// <summary>Tweens a 2D Toolkit TextMesh's dimensions to the given value.
  161. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  162. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  163. public static TweenerCore<Vector3, Vector3, VectorOptions> DOScaleZ(this tk2dTextMesh target, float endValue, float duration)
  164. {
  165. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.scale, x => target.scale = x, new Vector3(0, 0, endValue), duration);
  166. t.SetOptions(AxisConstraint.Z)
  167. .SetTarget(target);
  168. return t;
  169. }
  170. /// <summary>Tweens a 2D Toolkit TextMesh's color to the given value.
  171. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  172. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  173. public static TweenerCore<Color, Color, ColorOptions> DOColor(this tk2dTextMesh target, Color endValue, float duration)
  174. {
  175. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  176. t.SetTarget(target);
  177. return t;
  178. }
  179. /// <summary>Tweens a 2D Toolkit TextMesh's alpha color to the given value.
  180. /// Also stores the TextMesh as the tween's target so it can be used for filtered operations</summary>
  181. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  182. public static TweenerCore<Color, Color, ColorOptions> DOFade(this tk2dTextMesh target, float endValue, float duration)
  183. {
  184. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  185. t.SetTarget(target);
  186. return t;
  187. }
  188. /// <summary>Tweens a 2D Toolkit TextMesh's color using the given gradient
  189. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  190. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  191. /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
  192. public static Sequence DOGradientColor(this tk2dTextMesh target, Gradient gradient, float duration)
  193. {
  194. Sequence s = DOTween.Sequence();
  195. GradientColorKey[] colors = gradient.colorKeys;
  196. int len = colors.Length;
  197. for (int i = 0; i < len; ++i) {
  198. GradientColorKey c = colors[i];
  199. if (i == 0 && c.time <= 0) {
  200. target.color = c.color;
  201. continue;
  202. }
  203. float colorDuration = i == len - 1
  204. ? duration - s.Duration(false) // Verifies that total duration is correct
  205. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  206. s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
  207. }
  208. s.SetTarget(target);
  209. return s;
  210. }
  211. /// <summary>Tweens a tk2dTextMesh's text to the given value.
  212. /// Also stores the tk2dTextMesh as the tween's target so it can be used for filtered operations</summary>
  213. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  214. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  215. /// otherwise all tags will be considered as normal text</param>
  216. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  217. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  218. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  219. /// Leave it to NULL (default) to use default ones</param>
  220. public static TweenerCore<string, string, StringOptions> DOText(this tk2dTextMesh target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  221. {
  222. TweenerCore<string, string, StringOptions> t = DOTween.To(() => target.text, x => target.text = x, endValue, duration);
  223. t.SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  224. .SetTarget(target);
  225. return t;
  226. }
  227. #endregion
  228. }
  229. }
  230. #endif