// 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 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 => { target.flexibleWidth = x.x; target.flexibleHeight = x.y; }, 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 => { target.minWidth = x.x; target.minHeight = x.y; }, 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 => { target.preferredWidth = x.x; target.preferredHeight = x.y; }, 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); } #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 the string will appear from a random animation of characters /// 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 scramble = false, string scrambleChars = null) { return DOTween.To(() => target.text, x => target.text = x, endValue, duration) .SetOptions(scramble, scrambleChars) .SetTarget(target); } #endregion #endregion } }