DOTweenModuleUI.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. #if true && (UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER) // MODULE_MARKER
  4. using System;
  5. using System.Globalization;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using DG.Tweening.Core;
  9. using DG.Tweening.Core.Enums;
  10. using DG.Tweening.Plugins;
  11. using DG.Tweening.Plugins.Options;
  12. using Outline = UnityEngine.UI.Outline;
  13. using Text = UnityEngine.UI.Text;
  14. #pragma warning disable 1591
  15. namespace DG.Tweening
  16. {
  17. public static class DOTweenModuleUI
  18. {
  19. #region Shortcuts
  20. #region CanvasGroup
  21. /// <summary>Tweens a CanvasGroup's alpha color to the given value.
  22. /// Also stores the canvasGroup as the tween's target so it can be used for filtered operations</summary>
  23. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  24. public static TweenerCore<float, float, FloatOptions> DOFade(this CanvasGroup target, float endValue, float duration)
  25. {
  26. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.alpha, x => target.alpha = x, endValue, duration);
  27. t.SetTarget(target);
  28. return t;
  29. }
  30. #endregion
  31. #region Graphic
  32. /// <summary>Tweens an Graphic's color to the given value.
  33. /// Also stores the image 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 TweenerCore<Color, Color, ColorOptions> DOColor(this Graphic target, Color endValue, float duration)
  36. {
  37. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  38. t.SetTarget(target);
  39. return t;
  40. }
  41. /// <summary>Tweens an Graphic's alpha color to the given value.
  42. /// Also stores the image 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 TweenerCore<Color, Color, ColorOptions> DOFade(this Graphic target, float endValue, float duration)
  45. {
  46. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  47. t.SetTarget(target);
  48. return t;
  49. }
  50. #endregion
  51. #region Image
  52. /// <summary>Tweens an Image's color to the given value.
  53. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  54. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  55. public static TweenerCore<Color, Color, ColorOptions> DOColor(this Image target, Color endValue, float duration)
  56. {
  57. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  58. t.SetTarget(target);
  59. return t;
  60. }
  61. /// <summary>Tweens an Image's alpha color to the given value.
  62. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  63. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  64. public static TweenerCore<Color, Color, ColorOptions> DOFade(this Image target, float endValue, float duration)
  65. {
  66. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  67. t.SetTarget(target);
  68. return t;
  69. }
  70. /// <summary>Tweens an Image's fillAmount to the given value.
  71. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  72. /// <param name="endValue">The end value to reach (0 to 1)</param><param name="duration">The duration of the tween</param>
  73. public static TweenerCore<float, float, FloatOptions> DOFillAmount(this Image target, float endValue, float duration)
  74. {
  75. if (endValue > 1) endValue = 1;
  76. else if (endValue < 0) endValue = 0;
  77. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.fillAmount, x => target.fillAmount = x, endValue, duration);
  78. t.SetTarget(target);
  79. return t;
  80. }
  81. /// <summary>Tweens an Image's colors using the given gradient
  82. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  83. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  84. /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
  85. public static Sequence DOGradientColor(this Image target, Gradient gradient, float duration)
  86. {
  87. Sequence s = DOTween.Sequence();
  88. GradientColorKey[] colors = gradient.colorKeys;
  89. int len = colors.Length;
  90. for (int i = 0; i < len; ++i) {
  91. GradientColorKey c = colors[i];
  92. if (i == 0 && c.time <= 0) {
  93. target.color = c.color;
  94. continue;
  95. }
  96. float colorDuration = i == len - 1
  97. ? duration - s.Duration(false) // Verifies that total duration is correct
  98. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  99. s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
  100. }
  101. s.SetTarget(target);
  102. return s;
  103. }
  104. #endregion
  105. #region LayoutElement
  106. /// <summary>Tweens an LayoutElement's flexibleWidth/Height to the given value.
  107. /// Also stores the LayoutElement as the tween's target so it can be used for filtered operations</summary>
  108. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  109. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  110. public static TweenerCore<Vector2, Vector2, VectorOptions> DOFlexibleSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
  111. {
  112. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.flexibleWidth, target.flexibleHeight), x => {
  113. target.flexibleWidth = x.x;
  114. target.flexibleHeight = x.y;
  115. }, endValue, duration);
  116. t.SetOptions(snapping).SetTarget(target);
  117. return t;
  118. }
  119. /// <summary>Tweens an LayoutElement's minWidth/Height to the given value.
  120. /// Also stores the LayoutElement 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. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  123. public static TweenerCore<Vector2, Vector2, VectorOptions> DOMinSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
  124. {
  125. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.minWidth, target.minHeight), x => {
  126. target.minWidth = x.x;
  127. target.minHeight = x.y;
  128. }, endValue, duration);
  129. t.SetOptions(snapping).SetTarget(target);
  130. return t;
  131. }
  132. /// <summary>Tweens an LayoutElement's preferredWidth/Height to the given value.
  133. /// Also stores the LayoutElement 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. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  136. public static TweenerCore<Vector2, Vector2, VectorOptions> DOPreferredSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
  137. {
  138. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.preferredWidth, target.preferredHeight), x => {
  139. target.preferredWidth = x.x;
  140. target.preferredHeight = x.y;
  141. }, endValue, duration);
  142. t.SetOptions(snapping).SetTarget(target);
  143. return t;
  144. }
  145. #endregion
  146. #region Outline
  147. /// <summary>Tweens a Outline's effectColor to the given value.
  148. /// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
  149. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  150. public static TweenerCore<Color, Color, ColorOptions> DOColor(this Outline target, Color endValue, float duration)
  151. {
  152. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.effectColor, x => target.effectColor = x, endValue, duration);
  153. t.SetTarget(target);
  154. return t;
  155. }
  156. /// <summary>Tweens a Outline's effectColor alpha to the given value.
  157. /// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
  158. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  159. public static TweenerCore<Color, Color, ColorOptions> DOFade(this Outline target, float endValue, float duration)
  160. {
  161. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.effectColor, x => target.effectColor = x, endValue, duration);
  162. t.SetTarget(target);
  163. return t;
  164. }
  165. /// <summary>Tweens a Outline's effectDistance to the given value.
  166. /// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
  167. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  168. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScale(this Outline target, Vector2 endValue, float duration)
  169. {
  170. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.effectDistance, x => target.effectDistance = x, endValue, duration);
  171. t.SetTarget(target);
  172. return t;
  173. }
  174. #endregion
  175. #region RectTransform
  176. /// <summary>Tweens a RectTransform's anchoredPosition to the given value.
  177. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  178. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  179. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  180. public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPos(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
  181. {
  182. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, endValue, duration);
  183. t.SetOptions(snapping).SetTarget(target);
  184. return t;
  185. }
  186. /// <summary>Tweens a RectTransform's anchoredPosition X to the given value.
  187. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  188. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  189. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  190. public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPosX(this RectTransform target, float endValue, float duration, bool snapping = false)
  191. {
  192. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue, 0), duration);
  193. t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
  194. return t;
  195. }
  196. /// <summary>Tweens a RectTransform's anchoredPosition Y to the given value.
  197. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  198. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  199. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  200. public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPosY(this RectTransform target, float endValue, float duration, bool snapping = false)
  201. {
  202. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, endValue), duration);
  203. t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
  204. return t;
  205. }
  206. /// <summary>Tweens a RectTransform's anchoredPosition3D to the given value.
  207. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  208. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  209. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  210. public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3D(this RectTransform target, Vector3 endValue, float duration, bool snapping = false)
  211. {
  212. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, endValue, duration);
  213. t.SetOptions(snapping).SetTarget(target);
  214. return t;
  215. }
  216. /// <summary>Tweens a RectTransform's anchoredPosition3D X to the given value.
  217. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  218. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  219. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  220. public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DX(this RectTransform target, float endValue, float duration, bool snapping = false)
  221. {
  222. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(endValue, 0, 0), duration);
  223. t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
  224. return t;
  225. }
  226. /// <summary>Tweens a RectTransform's anchoredPosition3D Y to the given value.
  227. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  228. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  229. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  230. public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DY(this RectTransform target, float endValue, float duration, bool snapping = false)
  231. {
  232. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(0, endValue, 0), duration);
  233. t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
  234. return t;
  235. }
  236. /// <summary>Tweens a RectTransform's anchoredPosition3D Z to the given value.
  237. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  238. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  239. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  240. public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DZ(this RectTransform target, float endValue, float duration, bool snapping = false)
  241. {
  242. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(0, 0, endValue), duration);
  243. t.SetOptions(AxisConstraint.Z, snapping).SetTarget(target);
  244. return t;
  245. }
  246. /// <summary>Tweens a RectTransform's anchorMax to the given value.
  247. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  248. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  249. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  250. public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorMax(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
  251. {
  252. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchorMax, x => target.anchorMax = x, endValue, duration);
  253. t.SetOptions(snapping).SetTarget(target);
  254. return t;
  255. }
  256. /// <summary>Tweens a RectTransform's anchorMin to the given value.
  257. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  258. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  259. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  260. public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorMin(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
  261. {
  262. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchorMin, x => target.anchorMin = x, endValue, duration);
  263. t.SetOptions(snapping).SetTarget(target);
  264. return t;
  265. }
  266. /// <summary>Tweens a RectTransform's pivot to the given value.
  267. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  268. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  269. public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivot(this RectTransform target, Vector2 endValue, float duration)
  270. {
  271. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, endValue, duration);
  272. t.SetTarget(target);
  273. return t;
  274. }
  275. /// <summary>Tweens a RectTransform's pivot X to the given value.
  276. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  277. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  278. public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivotX(this RectTransform target, float endValue, float duration)
  279. {
  280. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, new Vector2(endValue, 0), duration);
  281. t.SetOptions(AxisConstraint.X).SetTarget(target);
  282. return t;
  283. }
  284. /// <summary>Tweens a RectTransform's pivot Y to the given value.
  285. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  286. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  287. public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivotY(this RectTransform target, float endValue, float duration)
  288. {
  289. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, new Vector2(0, endValue), duration);
  290. t.SetOptions(AxisConstraint.Y).SetTarget(target);
  291. return t;
  292. }
  293. /// <summary>Tweens a RectTransform's sizeDelta to the given value.
  294. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  295. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  296. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  297. public static TweenerCore<Vector2, Vector2, VectorOptions> DOSizeDelta(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
  298. {
  299. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.sizeDelta, x => target.sizeDelta = x, endValue, duration);
  300. t.SetOptions(snapping).SetTarget(target);
  301. return t;
  302. }
  303. /// <summary>Punches a RectTransform's anchoredPosition towards the given direction and then back to the starting one
  304. /// as if it was connected to the starting position via an elastic.
  305. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  306. /// <param name="punch">The direction and strength of the punch (added to the RectTransform's current position)</param>
  307. /// <param name="duration">The duration of the tween</param>
  308. /// <param name="vibrato">Indicates how much will the punch vibrate</param>
  309. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards.
  310. /// 1 creates a full oscillation between the punch direction and the opposite direction,
  311. /// while 0 oscillates only between the punch and the start position</param>
  312. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  313. public static Tweener DOPunchAnchorPos(this RectTransform target, Vector2 punch, float duration, int vibrato = 10, float elasticity = 1, bool snapping = false)
  314. {
  315. return DOTween.Punch(() => target.anchoredPosition, x => target.anchoredPosition = x, punch, duration, vibrato, elasticity)
  316. .SetTarget(target).SetOptions(snapping);
  317. }
  318. /// <summary>Shakes a RectTransform's anchoredPosition with the given values.
  319. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  320. /// <param name="duration">The duration of the tween</param>
  321. /// <param name="strength">The shake strength</param>
  322. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  323. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  324. /// Setting it to 0 will shake along a single direction.</param>
  325. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  326. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  327. public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, float strength = 100, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true)
  328. {
  329. return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, true, fadeOut)
  330. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  331. }
  332. /// <summary>Shakes a RectTransform's anchoredPosition with the given values.
  333. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  334. /// <param name="duration">The duration of the tween</param>
  335. /// <param name="strength">The shake strength on each axis</param>
  336. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  337. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  338. /// Setting it to 0 will shake along a single direction.</param>
  339. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  340. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  341. public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true)
  342. {
  343. return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, fadeOut)
  344. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  345. }
  346. #region Special
  347. /// <summary>Tweens a RectTransform's anchoredPosition to the given value, while also applying a jump effect along the Y axis.
  348. /// Returns a Sequence instead of a Tweener.
  349. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  350. /// <param name="endValue">The end value to reach</param>
  351. /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
  352. /// <param name="numJumps">Total number of jumps</param>
  353. /// <param name="duration">The duration of the tween</param>
  354. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  355. public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
  356. {
  357. if (numJumps < 1) numJumps = 1;
  358. float startPosY = 0;
  359. float offsetY = -1;
  360. bool offsetYSet = false;
  361. // Separate Y Tween so we can elaborate elapsedPercentage on that insted of on the Sequence
  362. // (in case users add a delay or other elements to the Sequence)
  363. Sequence s = DOTween.Sequence();
  364. Tween yTween = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, jumpPower), duration / (numJumps * 2))
  365. .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
  366. .SetLoops(numJumps * 2, LoopType.Yoyo)
  367. .OnStart(()=> startPosY = target.anchoredPosition.y);
  368. s.Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0), duration)
  369. .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
  370. ).Join(yTween)
  371. .SetTarget(target).SetEase(DOTween.defaultEaseType);
  372. s.OnUpdate(() => {
  373. if (!offsetYSet) {
  374. offsetYSet = true;
  375. offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
  376. }
  377. Vector2 pos = target.anchoredPosition;
  378. pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
  379. target.anchoredPosition = pos;
  380. });
  381. return s;
  382. }
  383. #endregion
  384. #endregion
  385. #region ScrollRect
  386. /// <summary>Tweens a ScrollRect's horizontal/verticalNormalizedPosition to the given value.
  387. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  388. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  389. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  390. public static Tweener DONormalizedPos(this ScrollRect target, Vector2 endValue, float duration, bool snapping = false)
  391. {
  392. return DOTween.To(() => new Vector2(target.horizontalNormalizedPosition, target.verticalNormalizedPosition),
  393. x => {
  394. target.horizontalNormalizedPosition = x.x;
  395. target.verticalNormalizedPosition = x.y;
  396. }, endValue, duration)
  397. .SetOptions(snapping).SetTarget(target);
  398. }
  399. /// <summary>Tweens a ScrollRect's horizontalNormalizedPosition to the given value.
  400. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  401. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  402. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  403. public static Tweener DOHorizontalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
  404. {
  405. return DOTween.To(() => target.horizontalNormalizedPosition, x => target.horizontalNormalizedPosition = x, endValue, duration)
  406. .SetOptions(snapping).SetTarget(target);
  407. }
  408. /// <summary>Tweens a ScrollRect's verticalNormalizedPosition to the given value.
  409. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  410. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  411. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  412. public static Tweener DOVerticalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
  413. {
  414. return DOTween.To(() => target.verticalNormalizedPosition, x => target.verticalNormalizedPosition = x, endValue, duration)
  415. .SetOptions(snapping).SetTarget(target);
  416. }
  417. #endregion
  418. #region Slider
  419. /// <summary>Tweens a Slider's value to the given value.
  420. /// Also stores the Slider as the tween's target so it can be used for filtered operations</summary>
  421. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  422. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  423. public static TweenerCore<float, float, FloatOptions> DOValue(this Slider target, float endValue, float duration, bool snapping = false)
  424. {
  425. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.value, x => target.value = x, endValue, duration);
  426. t.SetOptions(snapping).SetTarget(target);
  427. return t;
  428. }
  429. #endregion
  430. #region Text
  431. /// <summary>Tweens a Text's color to the given value.
  432. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  433. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  434. public static TweenerCore<Color, Color, ColorOptions> DOColor(this Text target, Color endValue, float duration)
  435. {
  436. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  437. t.SetTarget(target);
  438. return t;
  439. }
  440. /// <summary>
  441. /// Tweens a Text's text from one integer to another, with options for thousands separators
  442. /// </summary>
  443. /// <param name="fromValue">The value to start from</param>
  444. /// <param name="endValue">The end value to reach</param>
  445. /// <param name="duration">The duration of the tween</param>
  446. /// <param name="addThousandsSeparator">If TRUE (default) also adds thousands separators</param>
  447. /// <param name="culture">The <see cref="CultureInfo"/> to use (InvariantCulture if NULL)</param>
  448. public static TweenerCore<int, int, NoOptions> DOCounter(
  449. this Text target, int fromValue, int endValue, float duration, bool addThousandsSeparator = true, CultureInfo culture = null
  450. ){
  451. int v = fromValue;
  452. CultureInfo cInfo = !addThousandsSeparator ? null : culture ?? CultureInfo.InvariantCulture;
  453. TweenerCore<int, int, NoOptions> t = DOTween.To(() => v, x => {
  454. v = x;
  455. target.text = addThousandsSeparator
  456. ? v.ToString("N0", cInfo)
  457. : v.ToString();
  458. }, endValue, duration);
  459. t.SetTarget(target);
  460. return t;
  461. }
  462. /// <summary>Tweens a Text's alpha color to the given value.
  463. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  464. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  465. public static TweenerCore<Color, Color, ColorOptions> DOFade(this Text target, float endValue, float duration)
  466. {
  467. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  468. t.SetTarget(target);
  469. return t;
  470. }
  471. /// <summary>Tweens a Text's text to the given value.
  472. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  473. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  474. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  475. /// otherwise all tags will be considered as normal text</param>
  476. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  477. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  478. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  479. /// Leave it to NULL (default) to use default ones</param>
  480. public static TweenerCore<string, string, StringOptions> DOText(this Text target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  481. {
  482. if (endValue == null) {
  483. if (Debugger.logPriority > 0) Debugger.LogWarning("You can't pass a NULL string to DOText: an empty string will be used instead to avoid errors");
  484. endValue = "";
  485. }
  486. TweenerCore<string, string, StringOptions> t = DOTween.To(() => target.text, x => target.text = x, endValue, duration);
  487. t.SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  488. .SetTarget(target);
  489. return t;
  490. }
  491. #endregion
  492. #region Blendables
  493. #region Graphic
  494. /// <summary>Tweens a Graphic's color to the given value,
  495. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  496. /// instead than fight each other as multiple DOColor would do.
  497. /// Also stores the Graphic as the tween's target so it can be used for filtered operations</summary>
  498. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  499. public static Tweener DOBlendableColor(this Graphic target, Color endValue, float duration)
  500. {
  501. endValue = endValue - target.color;
  502. Color to = new Color(0, 0, 0, 0);
  503. return DOTween.To(() => to, x => {
  504. Color diff = x - to;
  505. to = x;
  506. target.color += diff;
  507. }, endValue, duration)
  508. .Blendable().SetTarget(target);
  509. }
  510. #endregion
  511. #region Image
  512. /// <summary>Tweens a Image's color to the given value,
  513. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  514. /// instead than fight each other as multiple DOColor would do.
  515. /// Also stores the Image as the tween's target so it can be used for filtered operations</summary>
  516. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  517. public static Tweener DOBlendableColor(this Image target, Color endValue, float duration)
  518. {
  519. endValue = endValue - target.color;
  520. Color to = new Color(0, 0, 0, 0);
  521. return DOTween.To(() => to, x => {
  522. Color diff = x - to;
  523. to = x;
  524. target.color += diff;
  525. }, endValue, duration)
  526. .Blendable().SetTarget(target);
  527. }
  528. #endregion
  529. #region Text
  530. /// <summary>Tweens a Text's color BY the given value,
  531. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  532. /// instead than fight each other as multiple DOColor would do.
  533. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  534. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  535. public static Tweener DOBlendableColor(this Text target, Color endValue, float duration)
  536. {
  537. endValue = endValue - target.color;
  538. Color to = new Color(0, 0, 0, 0);
  539. return DOTween.To(() => to, x => {
  540. Color diff = x - to;
  541. to = x;
  542. target.color += diff;
  543. }, endValue, duration)
  544. .Blendable().SetTarget(target);
  545. }
  546. #endregion
  547. #endregion
  548. #region Shapes
  549. /// <summary>Tweens a RectTransform's anchoredPosition so that it draws a circle around the given center.
  550. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations.<para/>
  551. /// IMPORTANT: SetFrom(value) requires a <see cref="Vector2"/> instead of a float, where the X property represents the "from degrees value"</summary>
  552. /// <param name="center">Circle-center/pivot around which to rotate (in UI anchoredPosition coordinates)</param>
  553. /// <param name="endValueDegrees">The end value degrees to reach (to rotate counter-clockwise pass a negative value)</param>
  554. /// <param name="duration">The duration of the tween</param>
  555. /// <param name="relativeCenter">If TRUE the <see cref="center"/> coordinates will be considered as relative to the target's current anchoredPosition</param>
  556. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  557. public static TweenerCore<Vector2, Vector2, CircleOptions> DOShapeCircle(
  558. this RectTransform target, Vector2 center, float endValueDegrees, float duration, bool relativeCenter = false, bool snapping = false
  559. )
  560. {
  561. TweenerCore<Vector2, Vector2, CircleOptions> t = DOTween.To(
  562. CirclePlugin.Get(), () => target.anchoredPosition, x => target.anchoredPosition = x, center, duration
  563. );
  564. t.SetOptions(endValueDegrees, relativeCenter, snapping).SetTarget(target);
  565. return t;
  566. }
  567. #endregion
  568. #endregion
  569. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  570. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  571. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  572. public static class Utils
  573. {
  574. /// <summary>
  575. /// Converts the anchoredPosition of the first RectTransform to the second RectTransform,
  576. /// taking into consideration offset, anchors and pivot, and returns the new anchoredPosition
  577. /// </summary>
  578. public static Vector2 SwitchToRectTransform(RectTransform from, RectTransform to)
  579. {
  580. Vector2 localPoint;
  581. Vector2 fromPivotDerivedOffset = new Vector2(from.rect.width * 0.5f + from.rect.xMin, from.rect.height * 0.5f + from.rect.yMin);
  582. Vector2 screenP = RectTransformUtility.WorldToScreenPoint(null, from.position);
  583. screenP += fromPivotDerivedOffset;
  584. RectTransformUtility.ScreenPointToLocalPointInRectangle(to, screenP, null, out localPoint);
  585. Vector2 pivotDerivedOffset = new Vector2(to.rect.width * 0.5f + to.rect.xMin, to.rect.height * 0.5f + to.rect.yMin);
  586. return to.anchoredPosition + localPoint - pivotDerivedOffset;
  587. }
  588. }
  589. }
  590. }
  591. #endif