DOTweenTextMeshPro.cs.addon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2015/03/27 19:02
  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. using TMPro;
  8. namespace DG.Tweening
  9. {
  10. /// <summary>
  11. /// Methods that extend Text Mesh Pro objects and allow to directly create and control tweens from their instances.
  12. /// </summary>
  13. public static class ShortcutExtensionsTextMeshPro
  14. {
  15. #region Colors
  16. /// <summary>Tweens a TextMeshPro's color to the given value.
  17. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  18. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  19. public static Tweener DOColor(this TextMeshPro target, Color endValue, float duration)
  20. {
  21. return DOTween.To(() => target.color, x => target.color = x, endValue, duration)
  22. .SetTarget(target);
  23. }
  24. /// <summary>Tweens a TextMeshPro's faceColor to the given value.
  25. /// Also stores the TextMeshPro 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 Tweener DOFaceColor(this TextMeshPro target, Color32 endValue, float duration)
  28. {
  29. return DOTween.To(() => target.faceColor, x => target.faceColor = x, endValue, duration)
  30. .SetTarget(target);
  31. }
  32. /// <summary>Tweens a TextMeshPro's outlineColor to the given value.
  33. /// Also stores the TextMeshPro 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 DOOutlineColor(this TextMeshPro target, Color32 endValue, float duration)
  36. {
  37. return DOTween.To(() => target.outlineColor, x => target.outlineColor = x, endValue, duration)
  38. .SetTarget(target);
  39. }
  40. /// <summary>Tweens a TextMeshPro's glow color to the given value.
  41. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  42. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  43. /// <param name="useSharedMaterial">If TRUE will use the fontSharedMaterial instead than the fontMaterial</param>
  44. public static Tweener DOGlowColor(this TextMeshPro target, Color endValue, float duration, bool useSharedMaterial = false)
  45. {
  46. return useSharedMaterial
  47. ? target.fontSharedMaterial.DOColor(endValue, "_GlowColor", duration).SetTarget(target)
  48. : target.fontMaterial.DOColor(endValue, "_GlowColor", duration).SetTarget(target);
  49. }
  50. /// <summary>Tweens a TextMeshPro's alpha color to the given value.
  51. /// Also stores the TextMeshPro 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 DOFade(this TextMeshPro target, float endValue, float duration)
  54. {
  55. return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration)
  56. .SetTarget(target);
  57. }
  58. /// <summary>Tweens a TextMeshPro faceColor's alpha to the given value.
  59. /// Also stores the TextMeshPro 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 DOFaceFade(this TextMeshPro target, float endValue, float duration)
  62. {
  63. return DOTween.ToAlpha(() => target.faceColor, x => target.faceColor = x, endValue, duration)
  64. .SetTarget(target);
  65. }
  66. #endregion
  67. #region Other
  68. /// <summary>Tweens a TextMeshPro's scale to the given value (using correct uniform scale as TMP requires).
  69. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  70. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  71. public static Tweener DOScale(this TextMeshPro target, float endValue, float duration)
  72. {
  73. Transform t = target.transform;
  74. Vector3 endValueV3 = new Vector3(endValue, endValue, endValue);
  75. return DOTween.To(() => t.localScale, x => t.localScale = x, endValueV3, duration).SetTarget(target);
  76. }
  77. /// <summary>Tweens a TextMeshPro's fontSize to the given value.
  78. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  79. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  80. public static Tweener DOFontSize(this TextMeshPro target, float endValue, float duration)
  81. {
  82. return DOTween.To(() => target.fontSize, x => target.fontSize = x, endValue, duration)
  83. .SetTarget(target);
  84. }
  85. /// <summary>Tweens a TextMeshPro's maxVisibleCharacters to the given value.
  86. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  87. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  88. public static Tweener DOMaxVisibleCharacters(this TextMeshPro target, int endValue, float duration)
  89. {
  90. return DOTween.To(() => target.maxVisibleCharacters, x => target.maxVisibleCharacters = x, endValue, duration)
  91. .SetTarget(target);
  92. }
  93. /// <summary>Tweens a TextMeshPro's text to the given value.
  94. /// Also stores the TextMeshPro as the tween's target so it can be used for filtered operations</summary>
  95. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  96. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  97. /// otherwise all tags will be considered as normal text</param>
  98. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  99. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  100. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  101. /// Leave it to NULL (default) to use default ones</param>
  102. public static Tweener DOText(this TextMeshPro target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  103. {
  104. return DOTween.To(() => target.text, x => target.text = x, endValue, duration)
  105. .SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  106. .SetTarget(target);
  107. }
  108. #endregion
  109. }
  110. /// <summary>
  111. /// Methods that extend Text Mesh Pro objects and allow to directly create and control tweens from their instances.
  112. /// </summary>
  113. public static class ShortcutExtensionsTextMeshProUGUI
  114. {
  115. #region Colors
  116. /// <summary>Tweens a TextMeshProUGUI's color to the given value.
  117. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  118. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  119. public static Tweener DOColor(this TextMeshProUGUI target, Color endValue, float duration)
  120. {
  121. return DOTween.To(() => target.color, x => target.color = x, endValue, duration)
  122. .SetTarget(target);
  123. }
  124. /// <summary>Tweens a TextMeshProUGUI's faceColor to the given value.
  125. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  126. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  127. public static Tweener DOFaceColor(this TextMeshProUGUI target, Color32 endValue, float duration)
  128. {
  129. return DOTween.To(() => target.faceColor, x => target.faceColor = x, endValue, duration)
  130. .SetTarget(target);
  131. }
  132. /// <summary>Tweens a TextMeshProUGUI's outlineColor to the given value.
  133. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  134. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  135. public static Tweener DOOutlineColor(this TextMeshProUGUI target, Color32 endValue, float duration)
  136. {
  137. return DOTween.To(() => target.outlineColor, x => target.outlineColor = x, endValue, duration)
  138. .SetTarget(target);
  139. }
  140. /// <summary>Tweens a TextMeshProUGUI's glow color to the given value.
  141. /// Also stores the TextMeshProUGUI 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. /// <param name="useSharedMaterial">If TRUE will use the fontSharedMaterial instead than the fontMaterial</param>
  144. public static Tweener DOGlowColor(this TextMeshProUGUI target, Color endValue, float duration, bool useSharedMaterial = false)
  145. {
  146. return useSharedMaterial
  147. ? target.fontSharedMaterial.DOColor(endValue, "_GlowColor", duration).SetTarget(target)
  148. : target.fontMaterial.DOColor(endValue, "_GlowColor", duration).SetTarget(target);
  149. }
  150. /// <summary>Tweens a TextMeshProUGUI's alpha color to the given value.
  151. /// Also stores the TextMeshProUGUI 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 Tweener DOFade(this TextMeshProUGUI target, float endValue, float duration)
  154. {
  155. return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration)
  156. .SetTarget(target);
  157. }
  158. /// <summary>Tweens a TextMeshProUGUI faceColor's alpha to the given value.
  159. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  160. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  161. public static Tweener DOFaceFade(this TextMeshProUGUI target, float endValue, float duration)
  162. {
  163. return DOTween.ToAlpha(() => target.faceColor, x => target.faceColor = x, endValue, duration)
  164. .SetTarget(target);
  165. }
  166. #endregion
  167. #region Other
  168. /// <summary>Tweens a TextMeshProUGUI's scale to the given value (using correct uniform scale as TMP requires).
  169. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  170. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  171. public static Tweener DOScale(this TextMeshProUGUI target, float endValue, float duration)
  172. {
  173. Transform t = target.transform;
  174. Vector3 endValueV3 = new Vector3(endValue, endValue, endValue);
  175. return DOTween.To(() => t.localScale, x => t.localScale = x, endValueV3, duration).SetTarget(target);
  176. }
  177. /// <summary>Tweens a TextMeshProUGUI's fontSize to the given value.
  178. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  179. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  180. public static Tweener DOFontSize(this TextMeshProUGUI target, float endValue, float duration)
  181. {
  182. return DOTween.To(() => target.fontSize, x => target.fontSize = x, endValue, duration)
  183. .SetTarget(target);
  184. }
  185. /// <summary>Tweens a TextMeshProUGUI's maxVisibleCharacters to the given value.
  186. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  187. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  188. public static Tweener DOMaxVisibleCharacters(this TextMeshProUGUI target, int endValue, float duration)
  189. {
  190. return DOTween.To(() => target.maxVisibleCharacters, x => target.maxVisibleCharacters = x, endValue, duration)
  191. .SetTarget(target);
  192. }
  193. /// <summary>Tweens a TextMeshProUGUI's text to the given value.
  194. /// Also stores the TextMeshProUGUI as the tween's target so it can be used for filtered operations</summary>
  195. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  196. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  197. /// otherwise all tags will be considered as normal text</param>
  198. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  199. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  200. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  201. /// Leave it to NULL (default) to use default ones</param>
  202. public static Tweener DOText(this TextMeshProUGUI target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  203. {
  204. return DOTween.To(() => target.text, x => target.text = x, endValue, duration)
  205. .SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  206. .SetTarget(target);
  207. }
  208. #endregion
  209. }
  210. }