using System.Globalization; using DG.Tweening.Core; using DG.Tweening.Core.Enums; using DG.Tweening.Plugins; using DG.Tweening.Plugins.Options; using UnityEngine; using UnityEngine.UI; namespace DG.Tweening { public static class DOTweenModuleUI { public static class Utils { public static Vector2 SwitchToRectTransform(RectTransform from, RectTransform to) { Vector2 vector = new Vector2(from.rect.width * 0.5f + from.rect.xMin, from.rect.height * 0.5f + from.rect.yMin); Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(null, from.position); screenPoint += vector; RectTransformUtility.ScreenPointToLocalPointInRectangle(to, screenPoint, null, out var localPoint); Vector2 vector2 = new Vector2(to.rect.width * 0.5f + to.rect.xMin, to.rect.height * 0.5f + to.rect.yMin); return to.anchoredPosition + localPoint - vector2; } } public static TweenerCore DOFade(this CanvasGroup target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.alpha, delegate(float x) { target.alpha = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOColor(this Graphic target, Color endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOFade(this Graphic target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.ToAlpha(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOColor(this Image target, Color endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOFade(this Image target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.ToAlpha(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOFillAmount(this Image target, float endValue, float duration) { if (endValue > 1f) { endValue = 1f; } else if (endValue < 0f) { endValue = 0f; } TweenerCore tweenerCore = DOTween.To(() => target.fillAmount, delegate(float x) { target.fillAmount = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static Sequence DOGradientColor(this Image target, Gradient gradient, float duration) { Sequence sequence = DOTween.Sequence(); GradientColorKey[] colorKeys = gradient.colorKeys; int num = colorKeys.Length; for (int i = 0; i < num; i++) { GradientColorKey gradientColorKey = colorKeys[i]; if (i == 0 && gradientColorKey.time <= 0f) { target.color = gradientColorKey.color; continue; } float duration2 = ((i == num - 1) ? (duration - sequence.Duration(includeLoops: false)) : (duration * ((i == 0) ? gradientColorKey.time : (gradientColorKey.time - colorKeys[i - 1].time)))); sequence.Append(target.DOColor(gradientColorKey.color, duration2).SetEase(Ease.Linear)); } sequence.SetTarget(target); return sequence; } public static TweenerCore DOFlexibleSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => new Vector2(target.flexibleWidth, target.flexibleHeight), delegate(Vector2 x) { target.flexibleWidth = x.x; target.flexibleHeight = x.y; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOMinSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => new Vector2(target.minWidth, target.minHeight), delegate(Vector2 x) { target.minWidth = x.x; target.minHeight = x.y; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOPreferredSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => new Vector2(target.preferredWidth, target.preferredHeight), delegate(Vector2 x) { target.preferredWidth = x.x; target.preferredHeight = x.y; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOColor(this Outline target, Color endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.effectColor, delegate(Color x) { target.effectColor = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOFade(this Outline target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.ToAlpha(() => target.effectColor, delegate(Color x) { target.effectColor = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOScale(this Outline target, Vector2 endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.effectDistance, delegate(Vector2 x) { target.effectDistance = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPos(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPosX(this RectTransform target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, new Vector2(endValue, 0f), duration); tweenerCore.SetOptions(AxisConstraint.X, snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPosY(this RectTransform target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, new Vector2(0f, endValue), duration); tweenerCore.SetOptions(AxisConstraint.Y, snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPos3D(this RectTransform target, Vector3 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition3D, delegate(Vector3 x) { target.anchoredPosition3D = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPos3DX(this RectTransform target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition3D, delegate(Vector3 x) { target.anchoredPosition3D = x; }, new Vector3(endValue, 0f, 0f), duration); tweenerCore.SetOptions(AxisConstraint.X, snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPos3DY(this RectTransform target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition3D, delegate(Vector3 x) { target.anchoredPosition3D = x; }, new Vector3(0f, endValue, 0f), duration); tweenerCore.SetOptions(AxisConstraint.Y, snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorPos3DZ(this RectTransform target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchoredPosition3D, delegate(Vector3 x) { target.anchoredPosition3D = x; }, new Vector3(0f, 0f, endValue), duration); tweenerCore.SetOptions(AxisConstraint.Z, snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorMax(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchorMax, delegate(Vector2 x) { target.anchorMax = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOAnchorMin(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.anchorMin, delegate(Vector2 x) { target.anchorMin = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOPivot(this RectTransform target, Vector2 endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.pivot, delegate(Vector2 x) { target.pivot = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOPivotX(this RectTransform target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.pivot, delegate(Vector2 x) { target.pivot = x; }, new Vector2(endValue, 0f), duration); tweenerCore.SetOptions(AxisConstraint.X).SetTarget(target); return tweenerCore; } public static TweenerCore DOPivotY(this RectTransform target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.pivot, delegate(Vector2 x) { target.pivot = x; }, new Vector2(0f, endValue), duration); tweenerCore.SetOptions(AxisConstraint.Y).SetTarget(target); return tweenerCore; } public static TweenerCore DOSizeDelta(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.sizeDelta, delegate(Vector2 x) { target.sizeDelta = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static Tweener DOPunchAnchorPos(this RectTransform target, Vector2 punch, float duration, int vibrato = 10, float elasticity = 1f, bool snapping = false) { return DOTween.Punch(() => target.anchoredPosition, delegate(Vector3 x) { target.anchoredPosition = x; }, punch, duration, vibrato, elasticity).SetTarget(target).SetOptions(snapping); } public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, float strength = 100f, int vibrato = 10, float randomness = 90f, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full) { return DOTween.Shake(() => target.anchoredPosition, delegate(Vector3 x) { target.anchoredPosition = x; }, duration, strength, vibrato, randomness, ignoreZAxis: true, fadeOut, randomnessMode).SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake) .SetOptions(snapping); } public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90f, bool snapping = false, bool fadeOut = true, ShakeRandomnessMode randomnessMode = ShakeRandomnessMode.Full) { return DOTween.Shake(() => target.anchoredPosition, delegate(Vector3 x) { target.anchoredPosition = x; }, duration, strength, vibrato, randomness, fadeOut, randomnessMode).SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake) .SetOptions(snapping); } 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 = 0f; float offsetY = -1f; bool offsetYSet = false; Sequence s = DOTween.Sequence(); Tween t = DOTween.To(() => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, new Vector2(0f, jumpPower), duration / (float)(numJumps * 2)).SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetRelative() .SetLoops(numJumps * 2, LoopType.Yoyo) .OnStart(delegate { startPosY = target.anchoredPosition.y; }); s.Append(DOTween.To(() => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, new Vector2(endValue.x, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)).Join(t).SetTarget(target) .SetEase(DOTween.defaultEaseType); s.OnUpdate(delegate { if (!offsetYSet) { offsetYSet = true; offsetY = (s.isRelative ? endValue.y : (endValue.y - startPosY)); } Vector2 anchoredPosition = target.anchoredPosition; anchoredPosition.y += DOVirtual.EasedValue(0f, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); target.anchoredPosition = anchoredPosition; }); return s; } public static Tweener DONormalizedPos(this ScrollRect target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => new Vector2(target.horizontalNormalizedPosition, target.verticalNormalizedPosition), delegate(Vector2 x) { target.horizontalNormalizedPosition = x.x; target.verticalNormalizedPosition = x.y; }, endValue, duration).SetOptions(snapping).SetTarget(target); } public static Tweener DOHorizontalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false) { return DOTween.To(() => target.horizontalNormalizedPosition, delegate(float x) { target.horizontalNormalizedPosition = x; }, endValue, duration).SetOptions(snapping).SetTarget(target); } public static Tweener DOVerticalNormalizedPos(this ScrollRect target, float endValue, float duration, bool snapping = false) { return DOTween.To(() => target.verticalNormalizedPosition, delegate(float x) { target.verticalNormalizedPosition = x; }, endValue, duration).SetOptions(snapping).SetTarget(target); } public static TweenerCore DOValue(this Slider target, float endValue, float duration, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(() => target.value, delegate(float x) { target.value = x; }, endValue, duration); tweenerCore.SetOptions(snapping).SetTarget(target); return tweenerCore; } public static TweenerCore DOColor(this Text target, Color endValue, float duration) { TweenerCore tweenerCore = DOTween.To(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOCounter(this Text target, int fromValue, int endValue, float duration, bool addThousandsSeparator = true, CultureInfo culture = null) { CultureInfo cInfo = ((!addThousandsSeparator) ? null : (culture ?? CultureInfo.InvariantCulture)); TweenerCore tweenerCore = DOTween.To(() => fromValue, delegate(int x) { fromValue = x; target.text = (addThousandsSeparator ? fromValue.ToString("N0", cInfo) : fromValue.ToString()); }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore DOFade(this Text target, float endValue, float duration) { TweenerCore tweenerCore = DOTween.ToAlpha(() => target.color, delegate(Color x) { target.color = x; }, endValue, duration); tweenerCore.SetTarget(target); return tweenerCore; } public static TweenerCore 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 tweenerCore = DOTween.To(() => target.text, delegate(string x) { target.text = x; }, endValue, duration); tweenerCore.SetOptions(richTextEnabled, scrambleMode, scrambleChars).SetTarget(target); return tweenerCore; } public static Tweener DOBlendableColor(this Graphic target, Color endValue, float duration) { endValue -= target.color; Color to = new Color(0f, 0f, 0f, 0f); return DOTween.To(() => to, delegate(Color x) { Color color = x - to; to = x; target.color += color; }, endValue, duration).Blendable().SetTarget(target); } public static Tweener DOBlendableColor(this Image target, Color endValue, float duration) { endValue -= target.color; Color to = new Color(0f, 0f, 0f, 0f); return DOTween.To(() => to, delegate(Color x) { Color color = x - to; to = x; target.color += color; }, endValue, duration).Blendable().SetTarget(target); } public static Tweener DOBlendableColor(this Text target, Color endValue, float duration) { endValue -= target.color; Color to = new Color(0f, 0f, 0f, 0f); return DOTween.To(() => to, delegate(Color x) { Color color = x - to; to = x; target.color += color; }, endValue, duration).Blendable().SetTarget(target); } public static TweenerCore DOShapeCircle(this RectTransform target, Vector2 center, float endValueDegrees, float duration, bool relativeCenter = false, bool snapping = false) { TweenerCore tweenerCore = DOTween.To(CirclePlugin.Get(), () => target.anchoredPosition, delegate(Vector2 x) { target.anchoredPosition = x; }, center, duration); tweenerCore.SetOptions(endValueDegrees, relativeCenter, snapping).SetTarget(target); return tweenerCore; } } }