DOTweenModuleUI.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. /// <param name="randomnessMode">Randomness mode</param>
  328. public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, float strength = 100, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
  329. {
  330. return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, true, fadeOut, randomnessMode)
  331. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  332. }
  333. /// <summary>Shakes a RectTransform's anchoredPosition with the given values.
  334. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  335. /// <param name="duration">The duration of the tween</param>
  336. /// <param name="strength">The shake strength on each axis</param>
  337. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  338. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  339. /// Setting it to 0 will shake along a single direction.</param>
  340. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  341. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  342. /// <param name="randomnessMode">Randomness mode</param>
  343. public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
  344. {
  345. return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, fadeOut, randomnessMode)
  346. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  347. }
  348. #region Special
  349. /// <summary>Tweens a RectTransform's anchoredPosition to the given value, while also applying a jump effect along the Y axis.
  350. /// Returns a Sequence instead of a Tweener.
  351. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
  352. /// <param name="endValue">The end value to reach</param>
  353. /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
  354. /// <param name="numJumps">Total number of jumps</param>
  355. /// <param name="duration">The duration of the tween</param>
  356. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  357. public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
  358. {
  359. if (numJumps < 1) numJumps = 1;
  360. float startPosY = 0;
  361. float offsetY = -1;
  362. bool offsetYSet = false;
  363. // Separate Y Tween so we can elaborate elapsedPercentage on that insted of on the Sequence
  364. // (in case users add a delay or other elements to the Sequence)
  365. Sequence s = DOTween.Sequence();
  366. Tween yTween = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, jumpPower), duration / (numJumps * 2))
  367. .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
  368. .SetLoops(numJumps * 2, LoopType.Yoyo)
  369. .OnStart(()=> startPosY = target.anchoredPosition.y);
  370. s.Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0), duration)
  371. .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
  372. ).Join(yTween)
  373. .SetTarget(target).SetEase(DOTween.defaultEaseType);
  374. s.OnUpdate(() => {
  375. if (!offsetYSet) {
  376. offsetYSet = true;
  377. offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
  378. }
  379. Vector2 pos = target.anchoredPosition;
  380. pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
  381. target.anchoredPosition = pos;
  382. });
  383. return s;
  384. }
  385. #endregion
  386. #endregion
  387. #region ScrollRect
  388. /// <summary>Tweens a ScrollRect's horizontal/verticalNormalizedPosition to the given value.
  389. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  390. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  391. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  392. public static Tweener DONormalizedPos(this ScrollRect target, Vector2 endValue, float duration, bool snapping = false)
  393. {
  394. return DOTween.To(() => new Vector2(target.horizontalNormalizedPosition, target.verticalNormalizedPosition),
  395. x => {
  396. target.horizontalNormalizedPosition = x.x;
  397. target.verticalNormalizedPosition = x.y;
  398. }, endValue, duration)
  399. .SetOptions(snapping).SetTarget(target);
  400. }
  401. /// <summary>Tweens a ScrollRect's horizontalNormalizedPosition to the given value.
  402. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  403. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  404. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  405. public static Tweener DOHorizontalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
  406. {
  407. return DOTween.To(() => target.horizontalNormalizedPosition, x => target.horizontalNormalizedPosition = x, endValue, duration)
  408. .SetOptions(snapping).SetTarget(target);
  409. }
  410. /// <summary>Tweens a ScrollRect's verticalNormalizedPosition to the given value.
  411. /// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
  412. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  413. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  414. public static Tweener DOVerticalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
  415. {
  416. return DOTween.To(() => target.verticalNormalizedPosition, x => target.verticalNormalizedPosition = x, endValue, duration)
  417. .SetOptions(snapping).SetTarget(target);
  418. }
  419. #endregion
  420. #region Slider
  421. /// <summary>Tweens a Slider's value to the given value.
  422. /// Also stores the Slider as the tween's target so it can be used for filtered operations</summary>
  423. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  424. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  425. public static TweenerCore<float, float, FloatOptions> DOValue(this Slider target, float endValue, float duration, bool snapping = false)
  426. {
  427. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.value, x => target.value = x, endValue, duration);
  428. t.SetOptions(snapping).SetTarget(target);
  429. return t;
  430. }
  431. #endregion
  432. #region Text
  433. /// <summary>Tweens a Text's color to the given value.
  434. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  435. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  436. public static TweenerCore<Color, Color, ColorOptions> DOColor(this Text target, Color endValue, float duration)
  437. {
  438. TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
  439. t.SetTarget(target);
  440. return t;
  441. }
  442. /// <summary>
  443. /// Tweens a Text's text from one integer to another, with options for thousands separators
  444. /// </summary>
  445. /// <param name="fromValue">The value to start from</param>
  446. /// <param name="endValue">The end value to reach</param>
  447. /// <param name="duration">The duration of the tween</param>
  448. /// <param name="addThousandsSeparator">If TRUE (default) also adds thousands separators</param>
  449. /// <param name="culture">The <see cref="CultureInfo"/> to use (InvariantCulture if NULL)</param>
  450. public static TweenerCore<int, int, NoOptions> DOCounter(
  451. this Text target, int fromValue, int endValue, float duration, bool addThousandsSeparator = true, CultureInfo culture = null
  452. ){
  453. int v = fromValue;
  454. CultureInfo cInfo = !addThousandsSeparator ? null : culture ?? CultureInfo.InvariantCulture;
  455. TweenerCore<int, int, NoOptions> t = DOTween.To(() => v, x => {
  456. v = x;
  457. target.text = addThousandsSeparator
  458. ? v.ToString("N0", cInfo)
  459. : v.ToString();
  460. }, endValue, duration);
  461. t.SetTarget(target);
  462. return t;
  463. }
  464. /// <summary>Tweens a Text's alpha color to the given value.
  465. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  466. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  467. public static TweenerCore<Color, Color, ColorOptions> DOFade(this Text target, float endValue, float duration)
  468. {
  469. TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
  470. t.SetTarget(target);
  471. return t;
  472. }
  473. /// <summary>Tweens a Text's text to the given value.
  474. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  475. /// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
  476. /// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
  477. /// otherwise all tags will be considered as normal text</param>
  478. /// <param name="scrambleMode">The type of scramble mode to use, if any</param>
  479. /// <param name="scrambleChars">A string containing the characters to use for scrambling.
  480. /// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
  481. /// Leave it to NULL (default) to use default ones</param>
  482. public static TweenerCore<string, string, StringOptions> DOText(this Text target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
  483. {
  484. if (endValue == null) {
  485. 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");
  486. endValue = "";
  487. }
  488. TweenerCore<string, string, StringOptions> t = DOTween.To(() => target.text, x => target.text = x, endValue, duration);
  489. t.SetOptions(richTextEnabled, scrambleMode, scrambleChars)
  490. .SetTarget(target);
  491. return t;
  492. }
  493. #endregion
  494. #region Blendables
  495. #region Graphic
  496. /// <summary>Tweens a Graphic's color to the given value,
  497. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  498. /// instead than fight each other as multiple DOColor would do.
  499. /// Also stores the Graphic as the tween's target so it can be used for filtered operations</summary>
  500. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  501. public static Tweener DOBlendableColor(this Graphic target, Color endValue, float duration)
  502. {
  503. endValue = endValue - target.color;
  504. Color to = new Color(0, 0, 0, 0);
  505. return DOTween.To(() => to, x => {
  506. Color diff = x - to;
  507. to = x;
  508. target.color += diff;
  509. }, endValue, duration)
  510. .Blendable().SetTarget(target);
  511. }
  512. #endregion
  513. #region Image
  514. /// <summary>Tweens a Image's color to the given value,
  515. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  516. /// instead than fight each other as multiple DOColor would do.
  517. /// Also stores the Image as the tween's target so it can be used for filtered operations</summary>
  518. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  519. public static Tweener DOBlendableColor(this Image target, Color endValue, float duration)
  520. {
  521. endValue = endValue - target.color;
  522. Color to = new Color(0, 0, 0, 0);
  523. return DOTween.To(() => to, x => {
  524. Color diff = x - to;
  525. to = x;
  526. target.color += diff;
  527. }, endValue, duration)
  528. .Blendable().SetTarget(target);
  529. }
  530. #endregion
  531. #region Text
  532. /// <summary>Tweens a Text's color BY the given value,
  533. /// in a way that allows other DOBlendableColor tweens to work together on the same target,
  534. /// instead than fight each other as multiple DOColor would do.
  535. /// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
  536. /// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
  537. public static Tweener DOBlendableColor(this Text target, Color endValue, float duration)
  538. {
  539. endValue = endValue - target.color;
  540. Color to = new Color(0, 0, 0, 0);
  541. return DOTween.To(() => to, x => {
  542. Color diff = x - to;
  543. to = x;
  544. target.color += diff;
  545. }, endValue, duration)
  546. .Blendable().SetTarget(target);
  547. }
  548. #endregion
  549. #endregion
  550. #region Shapes
  551. /// <summary>Tweens a RectTransform's anchoredPosition so that it draws a circle around the given center.
  552. /// Also stores the RectTransform as the tween's target so it can be used for filtered operations.<para/>
  553. /// IMPORTANT: SetFrom(value) requires a <see cref="Vector2"/> instead of a float, where the X property represents the "from degrees value"</summary>
  554. /// <param name="center">Circle-center/pivot around which to rotate (in UI anchoredPosition coordinates)</param>
  555. /// <param name="endValueDegrees">The end value degrees to reach (to rotate counter-clockwise pass a negative value)</param>
  556. /// <param name="duration">The duration of the tween</param>
  557. /// <param name="relativeCenter">If TRUE the <see cref="center"/> coordinates will be considered as relative to the target's current anchoredPosition</param>
  558. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  559. public static TweenerCore<Vector2, Vector2, CircleOptions> DOShapeCircle(
  560. this RectTransform target, Vector2 center, float endValueDegrees, float duration, bool relativeCenter = false, bool snapping = false
  561. )
  562. {
  563. TweenerCore<Vector2, Vector2, CircleOptions> t = DOTween.To(
  564. CirclePlugin.Get(), () => target.anchoredPosition, x => target.anchoredPosition = x, center, duration
  565. );
  566. t.SetOptions(endValueDegrees, relativeCenter, snapping).SetTarget(target);
  567. return t;
  568. }
  569. #endregion
  570. #endregion
  571. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  572. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  573. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  574. public static class Utils
  575. {
  576. /// <summary>
  577. /// Converts the anchoredPosition of the first RectTransform to the second RectTransform,
  578. /// taking into consideration offset, anchors and pivot, and returns the new anchoredPosition
  579. /// </summary>
  580. public static Vector2 SwitchToRectTransform(RectTransform from, RectTransform to)
  581. {
  582. Vector2 localPoint;
  583. Vector2 fromPivotDerivedOffset = new Vector2(from.rect.width * 0.5f + from.rect.xMin, from.rect.height * 0.5f + from.rect.yMin);
  584. Vector2 screenP = RectTransformUtility.WorldToScreenPoint(null, from.position);
  585. screenP += fromPivotDerivedOffset;
  586. RectTransformUtility.ScreenPointToLocalPointInRectangle(to, screenP, null, out localPoint);
  587. Vector2 pivotDerivedOffset = new Vector2(to.rect.width * 0.5f + to.rect.xMin, to.rect.height * 0.5f + to.rect.yMin);
  588. return to.anchoredPosition + localPoint - pivotDerivedOffset;
  589. }
  590. }
  591. }
  592. }
  593. #endif