using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
//动画扩展类
public static class AnimExtension
{
//数字格式化
private static string Format(double num, string format = "f1", bool symbol = false, string unit = "")
{
if (num > 0 && symbol)
{
return "+" + num.ToString(format) + unit;
}
else
{
return num.ToString(format) + unit;
}
}
///
/// 文本动画
///
/// 文本组件
/// 目标数值
/// 显示格式
/// 持续时间
/// 延迟时间
/// 正数前是否显示"+"号
public static Tweener DOTextAnim(this Text text, double num, float duration = 1f, string format = "f1", float delay = 0f, string unit = "", bool symbol = false)
{
if (text == null) return null;
double data = 0;
text.text = Format(data, format, symbol, unit);
return DOTween.To(() => data, (x) => { data = x; text.text = Format(data, format, symbol, unit); }, num, duration).SetDelay(delay);
}
///
/// 文本动画
///
/// 文本组件
/// 起始数值
/// 目标数值
/// 持续时间
/// 显示格式
/// 延迟事件
/// 单位
/// 是否显示"+"号
public static Tweener DOTextAnim(this Text text, double fromNum, double toNum, float duration = 1f, string format = "f1", float delay = 0f, string unit = "", bool symbol = false)
{
if (text == null) return null;
double data = fromNum;
text.text = Format(data, format, symbol);
return DOTween.To(() => data, (x) => { data = x; text.text = Format(data, format, symbol); }, toNum, duration).SetDelay(delay);
}
///
/// Slider 动画
///
/// Slider 组件
/// 目标数值
/// 持续时间
/// 延迟时间
public static Tweener DOSliderAnim(this Slider slider, float num, float duration = 1f, float delay = 0f)
{
if (slider == null) return null;
slider.value = 0;
return DOTween.To(() => slider.value, (x) => slider.value = x, num, duration).SetDelay(delay);
}
///
/// Image 的 fillAmount 动画
///
/// Image组件
/// 目标数值
/// 持续时间
/// 延迟时间
public static Tweener DOFillAnim(this Image image, float num, float duration = 1f, float delay = 0f)
{
if (image == null) return null;
image.fillAmount = 0;
return image.DOFillAmount(num, duration).SetDelay(delay);
}
///
/// Scroll 垂直滚动动画
///
/// Scroll 组件
/// 持续时间
/// 延迟时间
public static Tweener DOVerticalAnim(this ScrollRect scroll, float duration = 30f, float delay = 1f)
{
scroll.DOKill();
scroll.verticalNormalizedPosition = 1f;
return scroll.DOVerticalNormalizedPos(0, duration).SetEase(Ease.Linear).SetLoops(-1, LoopType.Restart).SetDelay(delay);
}
///
/// Scroll 水平滚动动画
///
/// Scroll 组件
/// 持续时间
/// 延迟时间
public static Tweener DOHorizontalAnim(this ScrollRect scroll, float duration = 30f, float delay = 1f)
{
scroll.DOKill();
scroll.horizontalNormalizedPosition = 0f;
return scroll.DOHorizontalNormalizedPos(1, duration).SetEase(Ease.Linear).SetLoops(-1, LoopType.Restart).SetDelay(delay);
}
///
/// 材质动画
///
/// 材质对象
/// 属性名称
/// 目标值
/// 初始值
/// 动画时间
/// 延迟
public static Tweener DoPropertyAnim(this Material material, string property, float endValue, float startValue = 0f, float duration = 1f, float delay = 0f)
{
if (material == null) { return null; }
if (!material.HasProperty(property)) { return null; }
material.SetFloat(property, startValue);
float data = startValue;
return DOTween.To(() => data, (x) => { data = x; material.SetFloat(property, x); }, endValue, duration).SetDelay(delay);
}
///
/// 材质颜色动画
///
/// 材质对象
/// 属性名称
/// 目标颜色
/// 动画时间
/// 延迟
public static Tweener DoColorAnim(this Material material, string property, Color endColor, float duration = 1f, float delay = 0f)
{
if (material == null) { return null; }
if (!material.HasProperty(property)) { return null; }
Color color = material.GetColor(property);
return DOTween.To(() => color, (x) => { color = x; material.SetColor(property, color); }, endColor, duration).SetDelay(delay);
}
}