1
0
mirror of https://github.com/Cardidi/dotween-upm-fork.git synced 2025-12-22 02:06:06 +08:00
2015-04-16 21:44:25 +02:00

363 lines
19 KiB
C#

// 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.Plugins.Options;
using UnityEngine;
using UnityEngine.UI;
namespace DG.Tweening
{
/// <summary>
/// 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.
/// </summary>
public static class ShortcutExtensions
{
#region Unity UI
#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 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
/// <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 Tweener DOColor(this Graphic target, Color endValue, float duration)
{
return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target);
}
/// <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 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
/// <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 Tweener DOColor(this Image target, Color endValue, float duration)
{
return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target);
}
/// <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 Tweener DOFade(this Image target, float endValue, float duration)
{
return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration)
.SetTarget(target);
}
/// <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 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
/// <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 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);
}
/// <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 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);
}
/// <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 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
/// <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 Tweener DOColor(this Outline target, Color endValue, float duration)
{
return DOTween.To(() => target.effectColor, x => target.effectColor = x, endValue, duration).SetTarget(target);
}
/// <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 Tweener DOFade(this Outline target, float endValue, float duration)
{
return DOTween.ToAlpha(() => target.effectColor, x => target.effectColor = x, endValue, duration)
.SetTarget(target);
}
/// <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 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
/// <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 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);
}
/// <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 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);
}
/// <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 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
/// <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 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
/// <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 Tweener DOColor(this Text target, Color endValue, float duration)
{
return DOTween.To(() => target.color, x => target.color = x, endValue, duration).SetTarget(target);
}
/// <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 Tweener DOFade(this Text target, float endValue, float duration)
{
return DOTween.ToAlpha(() => target.color, x => target.color = x, endValue, duration)
.SetTarget(target);
}
/// <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 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
/// <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);
}
/// <summary>Tweens a Graphic's alpha BY the given value (as if you chained a <code>SetRelative</code>),
/// in a way that allows other DOBlendableFadeBy tweens to work together on the same target,
/// instead than fight each other as multiple DOFade would do.
/// Also stores the Graphic as the tween's target so it can be used for filtered operations</summary>
/// <param name="byValue">The value to tween by</param><param name="duration">The duration of the tween</param>
public static Tweener DOBlendableFadeBy(this Graphic target, float byValue, float duration)
{
Color to = new Color(0, 0, 0, 0);
return DOTween.To(() => to, x => {
Color diff = x - to;
to.a = x.a;
target.color += diff;
}, new Color(0, 0, 0, byValue), 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);
}
/// <summary>Tweens a Image's alpha BY the given value (as if you chained a <code>SetRelative</code>),
/// in a way that allows other DOBlendableFadeBy tweens to work together on the same target,
/// instead than fight each other as multiple DOFade would do.
/// Also stores the Image as the tween's target so it can be used for filtered operations</summary>
/// <param name="byValue">The value to tween by</param><param name="duration">The duration of the tween</param>
public static Tweener DOBlendableFadeBy(this Image target, float byValue, float duration)
{
Color to = new Color(0, 0, 0, 0);
return DOTween.To(() => to, x => {
Color diff = x - to;
to.a = x.a;
target.color += diff;
}, new Color(0, 0, 0, byValue), 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);
}
/// <summary>Tweens a Text's alpha BY the given value (as if you chained a <code>SetRelative</code>),
/// in a way that allows other DOBlendableFadeBy tweens to work together on the same target,
/// instead than fight each other as multiple DOFade would do.
/// Also stores the Text as the tween's target so it can be used for filtered operations</summary>
/// <param name="byValue">The value to tween by</param><param name="duration">The duration of the tween</param>
public static Tweener DOBlendableFadeBy(this Text target, float byValue, float duration)
{
Color to = new Color(0, 0, 0, 0);
return DOTween.To(() => to, x => {
Color diff = x - to;
to.a = x.a;
target.color += diff;
}, new Color(0, 0, 0, byValue), duration)
.Blendable().SetTarget(target);
}
#endregion
#endregion
}
}