DOTweenModuleUI.cs 40 KB

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