-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSliderAnim.cs
81 lines (71 loc) · 1.8 KB
/
SliderAnim.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
namespace GameUtil
{
[RequireComponent(typeof(Slider))]
public class SliderAnim : MonoBehaviour
{
private Slider m_Slider;
public Slider Slider => m_Slider;
public float value
{
get => Slider.value;
set => ForceSetValue(value);
}
private float mEndValue;
private float mCurValue;
private Tween mTween;
public static implicit operator Slider(SliderAnim anim)
{
return anim.Slider;
}
public static SliderAnim Get(Slider slider)
{
if (!slider) return null;
var go = slider.gameObject;
var anim = go.GetComponent<SliderAnim>();
if (!anim)
anim = go.AddComponent<SliderAnim>();
return anim;
}
private void Awake()
{
m_Slider = gameObject.GetComponent<Slider>();
}
/// <param name="stayMax">true:会把大于1的整数最终值保留为1 反之为0</param>
public void SetProgress(float endValue, float? startValue = null, float duration = 0.3f,
TweenCallback onComplete = null, bool stayMax = false)
{
mTween?.Kill();
var remain = Mathf.FloorToInt(mEndValue) - Mathf.FloorToInt(mCurValue);
remain = remain > 0 ? remain : 0;
mEndValue = remain + endValue;
mCurValue = startValue ?? Slider.value;
mTween = DOTween.To(() => mCurValue, x =>
{
mCurValue = x;
SetValue(x, stayMax);
}, mEndValue, duration)
.OnComplete(onComplete)
.SetEase(Ease.OutQuad)
.SetUpdate(true);
}
/// <param name="stayMax">true:会把大于1的整数最终值保留为1 反之为0</param>
private void SetValue(float value, bool stayMax)
{
if (value >= 1)
{
value = value % 1;
if (stayMax)
value = value < 1e-6 ? 1 : value;
}
Slider.value = value;
}
private void ForceSetValue(float value)
{
mTween?.Kill();
SetValue(value, true);
}
}
}