DOTweenTk2d.cs.addon 13 KB

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