DOTweenModuleUIToolkit.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2026/01/08
  3. #if DOTWEEN_UITOOLKIT && UNITY_2021_3_OR_NEWER // MODULE_MARKER
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using DG.Tweening.Core;
  7. using DG.Tweening.Core.Enums;
  8. using DG.Tweening.Plugins.Options;
  9. #pragma warning disable 1591
  10. namespace DG.Tweening
  11. {
  12. public static class DOTweenModuleUIToolkit
  13. {
  14. #region Shortcuts
  15. #region VisualElement
  16. /// <summary>Tweens a VisualElement's position (via style.translate) to the given value.
  17. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  18. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  19. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  20. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMove(this VisualElement target, Vector3 endValue, float duration, bool snapping = false)
  21. {
  22. TweenerCore<Vector3, Vector3, VectorOptions> t
  23. = DOTween.To(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), endValue, duration);
  24. t.SetOptions(snapping).SetTarget(target);
  25. return t;
  26. }
  27. /// <summary>Tweens a VisualElement's position (via style.translate) to the given value.
  28. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  29. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  30. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  31. public static TweenerCore<Vector2, Vector2, VectorOptions> DOMove(this VisualElement target, Vector2 endValue, float duration, bool snapping = false)
  32. {
  33. TweenerCore<Vector2, Vector2, VectorOptions> t
  34. = DOTween.To(() => (Vector2)target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, 0), endValue, duration);
  35. t.SetOptions(snapping).SetTarget(target);
  36. return t;
  37. }
  38. /// <summary>Tweens a VisualElement's X position (via style.translate) to the given value.
  39. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  40. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  41. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  42. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveX(this VisualElement target, float endValue, float duration, bool snapping = false)
  43. {
  44. TweenerCore<Vector3, Vector3, VectorOptions> t
  45. = DOTween.To(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), new Vector3(endValue, 0, 0), duration);
  46. t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
  47. return t;
  48. }
  49. /// <summary>Tweens a VisualElement's Y position (via style.translate) to the given value.
  50. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  51. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  52. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  53. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveY(this VisualElement target, float endValue, float duration, bool snapping = false)
  54. {
  55. TweenerCore<Vector3, Vector3, VectorOptions> t
  56. = DOTween.To(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), new Vector3(0, endValue, 0), duration);
  57. t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
  58. return t;
  59. }
  60. /// <summary>Tweens a VisualElement's Z position (via style.translate) to the given value.
  61. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  62. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  63. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  64. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveZ(this VisualElement target, float endValue, float duration, bool snapping = false)
  65. {
  66. TweenerCore<Vector3, Vector3, VectorOptions> t
  67. = DOTween.To(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), new Vector3(0, 0, endValue), duration);
  68. t.SetOptions(AxisConstraint.Z, snapping).SetTarget(target);
  69. return t;
  70. }
  71. /// <summary>Tweens a VisualElement's scale (via style.scale) to the given value.
  72. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  73. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  74. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScale(this VisualElement target, Vector2 endValue, float duration)
  75. {
  76. TweenerCore<Vector2, Vector2, VectorOptions> t
  77. = DOTween.To(() => (Vector2)target.resolvedStyle.scale.value, x => target.style.scale = new Scale(x), endValue, duration);
  78. t.SetTarget(target);
  79. return t;
  80. }
  81. /// <summary>Tweens a VisualElement's scale (via style.scale) to the given value.
  82. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  83. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  84. public static TweenerCore<Vector2, Vector2, VectorOptions> DOScale(this VisualElement target, float endValue, float duration)
  85. {
  86. TweenerCore<Vector2, Vector2, VectorOptions> t
  87. = DOTween.To(() => (Vector2)target.resolvedStyle.scale.value, x => target.style.scale = new Scale(x), new Vector2(endValue, endValue), duration);
  88. t.SetTarget(target);
  89. return t;
  90. }
  91. /// <summary>Tweens a VisualElement's rotation (via style.rotate) to the given value.
  92. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  93. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  94. public static TweenerCore<float, float, FloatOptions> DORotate(this VisualElement target, float endValue, float duration)
  95. {
  96. TweenerCore<float, float, FloatOptions> t
  97. = DOTween.To(() => target.resolvedStyle.rotate.angle.value, x => target.style.rotate = new Rotate(x), endValue, duration);
  98. t.SetTarget(target);
  99. return t;
  100. }
  101. /// <summary>Punches a VisualElement's position towards the given direction and then back to the starting one
  102. /// as if it was connected to the starting position via an elastic.
  103. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  104. /// <param name="punch">The direction and strength of the punch (added to the VisualElement's current position)</param>
  105. /// <param name="duration">The duration of the tween</param>
  106. /// <param name="vibrato">Indicates how much will the punch vibrate</param>
  107. /// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards.
  108. /// 1 creates a full oscillation between the punch direction and the opposite direction,
  109. /// while 0 oscillates only between the punch and the start position</param>
  110. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  111. public static Tweener DOPunch(this VisualElement target, Vector3 punch, float duration, int vibrato = 10, float elasticity = 1, bool snapping = false)
  112. {
  113. return DOTween.Punch(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), punch, duration, vibrato, elasticity)
  114. .SetTarget(target).SetOptions(snapping);
  115. }
  116. /// <summary>Shakes a VisualElement's position with the given values.
  117. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  118. /// <param name="duration">The duration of the tween</param>
  119. /// <param name="strength">The shake strength</param>
  120. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  121. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  122. /// Setting it to 0 will shake along a single direction.</param>
  123. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  124. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  125. /// <param name="randomnessMode">Randomness mode</param>
  126. public static Tweener DOShake(this VisualElement target, float duration, float strength = 100, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
  127. {
  128. return DOTween.Shake(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), duration, strength, vibrato, randomness, true, fadeOut, randomnessMode)
  129. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  130. }
  131. /// <summary>Shakes a VisualElement's position with the given values.
  132. /// Also stores the VisualElement as the tween's target so it can be used for filtered operations</summary>
  133. /// <param name="duration">The duration of the tween</param>
  134. /// <param name="strength">The shake strength on each axis</param>
  135. /// <param name="vibrato">Indicates how much will the shake vibrate</param>
  136. /// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
  137. /// Setting it to 0 will shake along a single direction.</param>
  138. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  139. /// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
  140. /// <param name="randomnessMode">Randomness mode</param>
  141. public static Tweener DOShake(this VisualElement target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
  142. {
  143. return DOTween.Shake(() => target.resolvedStyle.translate, x => target.style.translate = new Translate(x.x, x.y, x.z), duration, strength, vibrato, randomness, fadeOut, randomnessMode)
  144. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
  145. }
  146. #endregion
  147. #endregion
  148. }
  149. }
  150. #endif