// Author: Daniele Giardini - http://www.demigiant.com // Created: 2014/09/29 11:53 // // License Copyright (c) Daniele Giardini. // This work is subject to the terms at http://dotween.demigiant.com/license.php using DG.Tweening.Core; using DG.Tweening.Core.Enums; using UnityEngine; using UnityEngine.UI; namespace DG.Tweening { /// /// Methods that extend known Unity objects and allow to directly create and control tweens from their instances. /// These, as all DOTween46 methods, require Unity 4.6 or later. /// public static class ShortcutExtensions { #region Unity UI #region CanvasGroup /// 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 /// The end value to reachThe duration of the tween public static Tweener DOFade(this CanvasGroup target, float endValue, float duration) { return DOTween.To(() => target.alpha, x => target.alpha = x, endValue, duration) .SetTarget(target); } #endregion #region Graphic /// 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 /// The end value to reachThe duration of the tween public static Tweener DOColor(this Graphic target, Color endValue, float duration) { return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween public static Tweener DOFade(this Graphic target, float endValue, float duration) { return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration) .SetTarget(target); } #endregion #region Image /// 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 /// The end value to reachThe duration of the tween public static Tweener DOColor(this Image target, Color endValue, float duration) { return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween public static Tweener DOFade(this Image target, float endValue, float duration) { return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration) .SetTarget(target); } /// 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 /// The end value to reach (0 to 1)The duration of the tween public static Tweener DOFillAmount(this Image target, float endValue, float duration) { if (endValue > 1) endValue = 1; else if (endValue < 0) endValue = 0; return DOTween.To(() => target.fillAmount, x => target.fillAmount = x, endValue, duration) .SetTarget(target); } #endregion #region LayoutElement /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOFlexibleSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => new Vector2(target.flexibleWidth, target.flexibleHeight), x => { #if COMPATIBLE target.flexibleWidth = x.value.x; target.flexibleHeight = x.value.y; #else target.flexibleWidth = x.x; target.flexibleHeight = x.y; #endif }, endValue, duration) .SetOptions(snapping).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOMinSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => new Vector2(target.minWidth, target.minHeight), x => { #if COMPATIBLE target.minWidth = x.value.x; target.minHeight = x.value.y; #else target.minWidth = x.x; target.minHeight = x.y; #endif }, endValue, duration) .SetOptions(snapping).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOPreferredSize(this LayoutElement target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => new Vector2(target.preferredWidth, target.preferredHeight), x => { #if COMPATIBLE target.preferredWidth = x.value.x; target.preferredHeight = x.value.y; #else target.preferredWidth = x.x; target.preferredHeight = x.y; #endif }, endValue, duration) .SetOptions(snapping).SetTarget(target); } #endregion #region Outline /// 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 /// The end value to reachThe duration of the tween public static Tweener DOColor(this Outline target, Color endValue, float duration) { return DOTween.To(() => target.effectColor, x => target.effectColor = x, endValue, duration).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween public static Tweener DOFade(this Outline target, float endValue, float duration) { return DOTween.ToAlpha(() => target.effectColor, x => target.effectColor = x, endValue, duration) .SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween public static Tweener DOScale(this Outline target, Vector2 endValue, float duration) { return DOTween.To(() => target.effectDistance, x => target.effectDistance = x, endValue, duration) .SetTarget(target); } #endregion #region RectTransform /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOAnchorPos(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, endValue, duration) .SetOptions(snapping).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOAnchorPos3D(this RectTransform target, Vector3 endValue, float duration, bool snapping = false) { return DOTween.To(() => target.anchoredPosition3D, x => target.anchoredPosition3D = x, endValue, duration) .SetOptions(snapping).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOSizeDelta(this RectTransform target, Vector2 endValue, float duration, bool snapping = false) { return DOTween.To(() => target.sizeDelta, x => target.sizeDelta = x, endValue, duration) .SetOptions(snapping).SetTarget(target); } /// 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 /// The duration of the tween /// The shake strength /// Indicates how much will the shake vibrate /// 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. /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, float strength = 100, int vibrato = 10, float randomness = 90, bool snapping = false) { return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness, true) .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping); } /// 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 /// The duration of the tween /// The shake strength on each axis /// Indicates how much will the shake vibrate /// 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. /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOShakeAnchorPos(this RectTransform target, float duration, Vector2 strength, int vibrato = 10, float randomness = 90, bool snapping = false) { return DOTween.Shake(() => target.anchoredPosition, x => target.anchoredPosition = x, duration, strength, vibrato, randomness) .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetShake).SetOptions(snapping); } #endregion #region Slider /// 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 /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Tweener DOValue(this Slider target, float endValue, float duration, bool snapping = false) { return DOTween.To(() => target.value, x => target.value = x, endValue, duration) .SetOptions(snapping).SetTarget(target); } #endregion #region Text /// 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 /// The end value to reachThe duration of the tween public static Tweener DOColor(this Text target, Color endValue, float duration) { return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target); } /// 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 /// The end value to reachThe duration of the tween public static Tweener DOFade(this Text target, float endValue, float duration) { return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration) .SetTarget(target); } /// 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 /// The end string to tween toThe duration of the tween /// If TRUE (default), rich text will be interpreted correctly while animated, /// otherwise all tags will be considered as normal text /// The type of scramble mode to use, if any /// 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 public static Tweener DOText(this Text target, string endValue, float duration, bool richTextEnabled = true, ScrambleMode scrambleMode = ScrambleMode.None, string scrambleChars = null) { return DOTween.To(() => target.text, x => target.text = x, endValue, duration) .SetOptions(richTextEnabled, scrambleMode, scrambleChars) .SetTarget(target); } #endregion #endregion #region Blendables #region Graphic /// 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 /// The value to tween toThe duration of the tween 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 => { #if COMPATIBLE Color diff = x.value - to; #else Color diff = x - to; #endif to = x; target.color += diff; }, endValue, duration) .Blendable().SetTarget(target); } #endregion #region Image /// 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 /// The value to tween toThe duration of the tween 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 => { #if COMPATIBLE Color diff = x.value - to; #else Color diff = x - to; #endif to = x; target.color += diff; }, endValue, duration) .Blendable().SetTarget(target); } #endregion #region Text /// 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 /// The value to tween toThe duration of the tween 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 => { #if COMPATIBLE Color diff = x.value - to; #else Color diff = x - to; #endif to = x; target.color += diff; }, endValue, duration) .Blendable().SetTarget(target); } #endregion #endregion } }