SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

609 lines
38 KiB

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