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