676 lines
42 KiB
C#
676 lines
42 KiB
C#
// Author: Daniele Giardini - http://www.demigiant.com
|
|
// Created: 2018/07/13
|
|
|
|
#if true // MODULE_MARKER
|
|
|
|
using DG.Tweening.Core;
|
|
using DG.Tweening.Core.Enums;
|
|
using DG.Tweening.Plugins;
|
|
using DG.Tweening.Plugins.Options;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Outline = UnityEngine.UI.Outline;
|
|
using Text = UnityEngine.UI.Text;
|
|
|
|
#pragma warning disable 1591
|
|
namespace DG.Tweening
|
|
{
|
|
public static class DOTweenModuleUI
|
|
{
|
|
#region Shortcuts
|
|
|
|
#region CanvasGroup
|
|
|
|
/// <summary>Tweens a CanvasGroup's alpha color to the given value.
|
|
/// Also stores the canvasGroup as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<float, float, FloatOptions> DOFade(this CanvasGroup target, float endValue, float duration)
|
|
{
|
|
TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.alpha, x => target.alpha = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Graphic
|
|
|
|
/// <summary>Tweens an Graphic's color to the given value.
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOColor(this Graphic target, Color endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an Graphic's alpha color to the given value.
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOFade(this Graphic target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Image
|
|
|
|
/// <summary>Tweens an Image's color to the given value.
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOColor(this Image target, Color endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an Image's alpha color to the given value.
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOFade(this Image target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an Image's fillAmount to the given value.
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach (0 to 1)</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<float, float, FloatOptions> DOFillAmount(this Image target, float endValue, float duration)
|
|
{
|
|
if (endValue > 1) endValue = 1;
|
|
else if (endValue < 0) endValue = 0;
|
|
TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.fillAmount, x => target.fillAmount = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an Image's colors using the given gradient
|
|
/// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
|
|
/// Also stores the image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
|
|
public static Sequence DOGradientColor(this Image target, Gradient gradient, float duration)
|
|
{
|
|
Sequence s = DOTween.Sequence();
|
|
GradientColorKey[] colors = gradient.colorKeys;
|
|
int len = colors.Length;
|
|
for (int i = 0; i < len; ++i)
|
|
{
|
|
GradientColorKey c = colors[i];
|
|
if (i == 0 && c.time <= 0)
|
|
{
|
|
target.color = c.color;
|
|
continue;
|
|
}
|
|
float colorDuration = i == len - 1
|
|
? duration - s.Duration(false) // Verifies that total duration is correct
|
|
: duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
|
|
s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
|
|
}
|
|
s.SetTarget(target);
|
|
return s;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LayoutElement
|
|
|
|
/// <summary>Tweens an LayoutElement's flexibleWidth/Height to the given value.
|
|
/// Also stores the LayoutElement as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOFlexibleSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.flexibleWidth, target.flexibleHeight), x =>
|
|
{
|
|
target.flexibleWidth = x.x;
|
|
target.flexibleHeight = x.y;
|
|
}, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an LayoutElement's minWidth/Height to the given value.
|
|
/// Also stores the LayoutElement as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOMinSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.minWidth, target.minHeight), x =>
|
|
{
|
|
target.minWidth = x.x;
|
|
target.minHeight = x.y;
|
|
}, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens an LayoutElement's preferredWidth/Height to the given value.
|
|
/// Also stores the LayoutElement as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOPreferredSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => new Vector2(target.preferredWidth, target.preferredHeight), x =>
|
|
{
|
|
target.preferredWidth = x.x;
|
|
target.preferredHeight = x.y;
|
|
}, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Outline
|
|
|
|
/// <summary>Tweens a Outline's effectColor to the given value.
|
|
/// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOColor(this Outline target, Color endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.effectColor, x => target.effectColor = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a Outline's effectColor alpha to the given value.
|
|
/// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOFade(this Outline target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.effectColor, x => target.effectColor = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a Outline's effectDistance to the given value.
|
|
/// Also stores the Outline as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOScale(this Outline target, Vector2 endValue, float duration)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.effectDistance, x => target.effectDistance = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region RectTransform
|
|
|
|
/// <summary>Tweens a RectTransform's anchoredPosition to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPos(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's anchoredPosition X to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPosX(this RectTransform target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue, 0), duration);
|
|
t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's anchoredPosition Y to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorPosY(this RectTransform target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, endValue), duration);
|
|
t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a RectTransform's anchoredPosition3D to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3D(this RectTransform target, Vector3 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's anchoredPosition3D X to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DX(this RectTransform target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(endValue, 0, 0), duration);
|
|
t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's anchoredPosition3D Y to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DY(this RectTransform target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(0, endValue, 0), duration);
|
|
t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's anchoredPosition3D Z to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector3, Vector3, VectorOptions> DOAnchorPos3DZ(this RectTransform target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, new Vector3(0, 0, endValue), duration);
|
|
t.SetOptions(AxisConstraint.Z, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a RectTransform's anchorMax to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorMax(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchorMax, x => target.anchorMax = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a RectTransform's anchorMin to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOAnchorMin(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.anchorMin, x => target.anchorMin = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a RectTransform's pivot to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivot(this RectTransform target, Vector2 endValue, float duration)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's pivot X to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivotX(this RectTransform target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, new Vector2(endValue, 0), duration);
|
|
t.SetOptions(AxisConstraint.X).SetTarget(target);
|
|
return t;
|
|
}
|
|
/// <summary>Tweens a RectTransform's pivot Y to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOPivotY(this RectTransform target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.pivot, x => target.pivot = x, new Vector2(0, endValue), duration);
|
|
t.SetOptions(AxisConstraint.Y).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a RectTransform's sizeDelta to the given value.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, VectorOptions> DOSizeDelta(this RectTransform target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.sizeDelta, x => target.sizeDelta = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Punches a RectTransform's anchoredPosition towards the given direction and then back to the starting one
|
|
/// as if it was connected to the starting position via an elastic.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="punch">The direction and strength of the punch (added to the RectTransform's current position)</param>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="vibrato">Indicates how much will the punch vibrate</param>
|
|
/// <param name="elasticity">Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards.
|
|
/// 1 creates a full oscillation between the punch direction and the opposite direction,
|
|
/// while 0 oscillates only between the punch and the start position</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static Tweener DOPunchAnchorPos(this RectTransform target, Vector2 punch, float duration, int vibrato = 10, float elasticity = 1, bool snapping = false)
|
|
{
|
|
return DOTween.Punch(() => target.anchoredPosition, x => target.anchoredPosition = x, punch, duration, vibrato, elasticity)
|
|
.SetTarget(target).SetOptions(snapping);
|
|
}
|
|
|
|
/// <summary>Shakes a RectTransform's anchoredPosition with the given values.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="strength">The shake strength</param>
|
|
/// <param name="vibrato">Indicates how much will the shake vibrate</param>
|
|
/// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
|
|
/// Setting it to 0 will shake along a single direction.</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
/// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
|
|
/// <param name="randomnessMode">Randomness mode</param>
|
|
public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, float strength = 100, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
|
|
{
|
|
return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, true, fadeOut, randomnessMode)
|
|
.SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
|
|
}
|
|
/// <summary>Shakes a RectTransform's anchoredPosition with the given values.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="strength">The shake strength on each axis</param>
|
|
/// <param name="vibrato">Indicates how much will the shake vibrate</param>
|
|
/// <param name="randomness">Indicates how much the shake will be random (0 to 180 - values higher than 90 kind of suck, so beware).
|
|
/// Setting it to 0 will shake along a single direction.</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
/// <param name="fadeOut">If TRUE the shake will automatically fadeOut smoothly within the tween's duration, otherwise it will not</param>
|
|
/// <param name="randomnessMode">Randomness mode</param>
|
|
public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full)
|
|
{
|
|
return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, fadeOut, randomnessMode)
|
|
.SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping);
|
|
}
|
|
|
|
#region Special
|
|
|
|
/// <summary>Tweens a RectTransform's anchoredPosition to the given value, while also applying a jump effect along the Y axis.
|
|
/// Returns a Sequence instead of a Tweener.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param>
|
|
/// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
|
|
/// <param name="numJumps">Total number of jumps</param>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
|
|
{
|
|
if (numJumps < 1) numJumps = 1;
|
|
float startPosY = 0;
|
|
float offsetY = -1;
|
|
bool offsetYSet = false;
|
|
|
|
// Separate Y Tween so we can elaborate elapsedPercentage on that insted of on the Sequence
|
|
// (in case users add a delay or other elements to the Sequence)
|
|
Sequence s = DOTween.Sequence();
|
|
Tween yTween = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, jumpPower), duration / (numJumps * 2))
|
|
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
|
|
.SetLoops(numJumps * 2, LoopType.Yoyo)
|
|
.OnStart(() => startPosY = target.anchoredPosition.y);
|
|
s.Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0), duration)
|
|
.SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
|
|
).Join(yTween)
|
|
.SetTarget(target).SetEase(DOTween.defaultEaseType);
|
|
s.OnUpdate(() =>
|
|
{
|
|
if (!offsetYSet)
|
|
{
|
|
offsetYSet = true;
|
|
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
|
|
}
|
|
Vector2 pos = target.anchoredPosition;
|
|
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
|
|
target.anchoredPosition = pos;
|
|
});
|
|
return s;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region ScrollRect
|
|
|
|
/// <summary>Tweens a ScrollRect's horizontal/verticalNormalizedPosition to the given value.
|
|
/// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static Tweener DONormalizedPos(this ScrollRect target, Vector2 endValue, float duration, bool snapping = false)
|
|
{
|
|
return DOTween.To(() => new Vector2(target.horizontalNormalizedPosition, target.verticalNormalizedPosition),
|
|
x =>
|
|
{
|
|
target.horizontalNormalizedPosition = x.x;
|
|
target.verticalNormalizedPosition = x.y;
|
|
}, endValue, duration)
|
|
.SetOptions(snapping).SetTarget(target);
|
|
}
|
|
/// <summary>Tweens a ScrollRect's horizontalNormalizedPosition to the given value.
|
|
/// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static Tweener DOHorizontalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
return DOTween.To(() => target.horizontalNormalizedPosition, x => target.horizontalNormalizedPosition = x, endValue, duration)
|
|
.SetOptions(snapping).SetTarget(target);
|
|
}
|
|
/// <summary>Tweens a ScrollRect's verticalNormalizedPosition to the given value.
|
|
/// Also stores the ScrollRect as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static Tweener DOVerticalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
return DOTween.To(() => target.verticalNormalizedPosition, x => target.verticalNormalizedPosition = x, endValue, duration)
|
|
.SetOptions(snapping).SetTarget(target);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Slider
|
|
|
|
/// <summary>Tweens a Slider's value to the given value.
|
|
/// Also stores the Slider as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<float, float, FloatOptions> DOValue(this Slider target, float endValue, float duration, bool snapping = false)
|
|
{
|
|
TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.value, x => target.value = x, endValue, duration);
|
|
t.SetOptions(snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Text
|
|
|
|
/// <summary>Tweens a Text's color to the given value.
|
|
/// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOColor(this Text target, Color endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.To(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tweens a Text's text from one integer to another, with options for thousands separators
|
|
/// </summary>
|
|
/// <param name="fromValue">The value to start from</param>
|
|
/// <param name="endValue">The end value to reach</param>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="addThousandsSeparator">If TRUE (default) also adds thousands separators</param>
|
|
/// <param name="culture">The <see cref="CultureInfo"/> to use (InvariantCulture if NULL)</param>
|
|
public static TweenerCore<int, int, NoOptions> DOCounter(
|
|
this Text target, int fromValue, int endValue, float duration, bool addThousandsSeparator = true, CultureInfo culture = null
|
|
)
|
|
{
|
|
int v = fromValue;
|
|
CultureInfo cInfo = !addThousandsSeparator ? null : culture ?? CultureInfo.InvariantCulture;
|
|
TweenerCore<int, int, NoOptions> t = DOTween.To(() => v, x =>
|
|
{
|
|
v = x;
|
|
target.text = addThousandsSeparator
|
|
? v.ToString("N0", cInfo)
|
|
: v.ToString();
|
|
}, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a Text's alpha color to the given value.
|
|
/// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
|
|
public static TweenerCore<Color, Color, ColorOptions> DOFade(this Text target, float endValue, float duration)
|
|
{
|
|
TweenerCore<Color, Color, ColorOptions> t = DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration);
|
|
t.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
/// <summary>Tweens a Text's text to the given value.
|
|
/// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The end string to tween to</param><param name="duration">The duration of the tween</param>
|
|
/// <param name="richTextEnabled">If TRUE (default), rich text will be interpreted correctly while animated,
|
|
/// otherwise all tags will be considered as normal text</param>
|
|
/// <param name="scrambleMode">The type of scramble mode to use, if any</param>
|
|
/// <param name="scrambleChars">A string containing the characters to use for scrambling.
|
|
/// Use as many characters as possible (minimum 10) because DOTween uses a fast scramble mode which gives better results with more characters.
|
|
/// Leave it to NULL (default) to use default ones</param>
|
|
public static TweenerCore<string, string, StringOptions> DOText(this Text target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null)
|
|
{
|
|
if (endValue == null)
|
|
{
|
|
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");
|
|
endValue = "";
|
|
}
|
|
TweenerCore<string, string, StringOptions> t = DOTween.To(() => target.text, x => target.text = x, endValue, duration);
|
|
t.SetOptions(richTextEnabled, scrambleMode, scrambleChars)
|
|
.SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Blendables
|
|
|
|
#region Graphic
|
|
|
|
/// <summary>Tweens a Graphic's color to the given value,
|
|
/// in a way that allows other DOBlendableColor tweens to work together on the same target,
|
|
/// instead than fight each other as multiple DOColor would do.
|
|
/// Also stores the Graphic as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
|
|
public static Tweener DOBlendableColor(this Graphic target, Color endValue, float duration)
|
|
{
|
|
endValue = endValue - target.color;
|
|
Color to = new Color(0, 0, 0, 0);
|
|
return DOTween.To(() => to, x =>
|
|
{
|
|
Color diff = x - to;
|
|
to = x;
|
|
target.color += diff;
|
|
}, endValue, duration)
|
|
.Blendable().SetTarget(target);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Image
|
|
|
|
/// <summary>Tweens a Image's color to the given value,
|
|
/// in a way that allows other DOBlendableColor tweens to work together on the same target,
|
|
/// instead than fight each other as multiple DOColor would do.
|
|
/// Also stores the Image as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
|
|
public static Tweener DOBlendableColor(this Image target, Color endValue, float duration)
|
|
{
|
|
endValue = endValue - target.color;
|
|
Color to = new Color(0, 0, 0, 0);
|
|
return DOTween.To(() => to, x =>
|
|
{
|
|
Color diff = x - to;
|
|
to = x;
|
|
target.color += diff;
|
|
}, endValue, duration)
|
|
.Blendable().SetTarget(target);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Text
|
|
|
|
/// <summary>Tweens a Text's color BY the given value,
|
|
/// in a way that allows other DOBlendableColor tweens to work together on the same target,
|
|
/// instead than fight each other as multiple DOColor would do.
|
|
/// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
|
|
/// <param name="endValue">The value to tween to</param><param name="duration">The duration of the tween</param>
|
|
public static Tweener DOBlendableColor(this Text target, Color endValue, float duration)
|
|
{
|
|
endValue = endValue - target.color;
|
|
Color to = new Color(0, 0, 0, 0);
|
|
return DOTween.To(() => to, x =>
|
|
{
|
|
Color diff = x - to;
|
|
to = x;
|
|
target.color += diff;
|
|
}, endValue, duration)
|
|
.Blendable().SetTarget(target);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Shapes
|
|
|
|
/// <summary>Tweens a RectTransform's anchoredPosition so that it draws a circle around the given center.
|
|
/// Also stores the RectTransform as the tween's target so it can be used for filtered operations.<para/>
|
|
/// IMPORTANT: SetFrom(value) requires a <see cref="Vector2"/> instead of a float, where the X property represents the "from degrees value"</summary>
|
|
/// <param name="center">Circle-center/pivot around which to rotate (in UI anchoredPosition coordinates)</param>
|
|
/// <param name="endValueDegrees">The end value degrees to reach (to rotate counter-clockwise pass a negative value)</param>
|
|
/// <param name="duration">The duration of the tween</param>
|
|
/// <param name="relativeCenter">If TRUE the <see cref="center"/> coordinates will be considered as relative to the target's current anchoredPosition</param>
|
|
/// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
|
|
public static TweenerCore<Vector2, Vector2, CircleOptions> DOShapeCircle(
|
|
this RectTransform target, Vector2 center, float endValueDegrees, float duration, bool relativeCenter = false, bool snapping = false
|
|
)
|
|
{
|
|
TweenerCore<Vector2, Vector2, CircleOptions> t = DOTween.To(
|
|
CirclePlugin.Get(), () => target.anchoredPosition, x => target.anchoredPosition = x, center, duration
|
|
);
|
|
t.SetOptions(endValueDegrees, relativeCenter, snapping).SetTarget(target);
|
|
return t;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
|
|
// ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
|
|
// █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
|
|
|
|
public static class Utils
|
|
{
|
|
/// <summary>
|
|
/// Converts the anchoredPosition of the first RectTransform to the second RectTransform,
|
|
/// taking into consideration offset, anchors and pivot, and returns the new anchoredPosition
|
|
/// </summary>
|
|
public static Vector2 SwitchToRectTransform(RectTransform from, RectTransform to)
|
|
{
|
|
Vector2 localPoint;
|
|
Vector2 fromPivotDerivedOffset = new Vector2(from.rect.width * 0.5f + from.rect.xMin, from.rect.height * 0.5f + from.rect.yMin);
|
|
Vector2 screenP = RectTransformUtility.WorldToScreenPoint(null, from.position);
|
|
screenP += fromPivotDerivedOffset;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(to, screenP, null, out localPoint);
|
|
Vector2 pivotDerivedOffset = new Vector2(to.rect.width * 0.5f + to.rect.xMin, to.rect.height * 0.5f + to.rect.yMin);
|
|
return to.anchoredPosition + localPoint - pivotDerivedOffset;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|