DOTweenModuleUI.cs 38 KB

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