-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathButtonAnim.cs
128 lines (110 loc) · 3.98 KB
/
ButtonAnim.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace GameUtil
{
public class ButtonAnim : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
[Serializable]
public class TweenSetting
{
public float Duration;
public bool UseCurve;
public AnimationCurve Curve;
public Ease EaseType;
public Vector3 Scale;
public TweenSetting() { }
public TweenSetting(float duration, bool useCurve, AnimationCurve curve, Ease easeType, Vector3 scale)
{
Duration = duration;
UseCurve = useCurve;
Curve = curve;
EaseType = easeType;
Scale = scale;
}
}
[SerializeField] private Transform m_ScaleTransform;
[SerializeField] private Button m_Button;
public bool RelativeScale = true;
public TweenSetting DownSetting = new TweenSetting(0.1f, false, AnimationCurve.Linear(0,0,1,1), Ease.OutQuad, new Vector3(0.95f, 0.95f, 1));
public TweenSetting UpSetting = new TweenSetting(0.1f, false, AnimationCurve.Linear(0,0,1,1), Ease.OutQuad, new Vector3(1.02f, 1.02f, 1));
public bool NeedSecondUpSetting = true;
public TweenSetting SecondUpSetting = new TweenSetting(0.08f, false, AnimationCurve.Linear(0,0,1,1), Ease.OutQuad, new Vector3(1, 1, 1));
private Vector3 mStartScale;
private Tween mTween;
private bool mIsPress;
private bool mIsStay;
private bool mIsEnable = true;
private bool mInteractable => !m_Button || m_Button.gameObject.activeSelf && m_Button.enabled && m_Button.interactable;
private void Awake()
{
if (!m_Button)
m_Button = gameObject.GetComponent<Button>();
if (!m_ScaleTransform)
m_ScaleTransform = transform;
mStartScale = m_ScaleTransform.localScale;
}
public void SetAnimEnable(bool enable)
{
if (mIsEnable == enable) return;
mIsEnable = enable;
if (mIsEnable) return;
Recover();
}
public void Recover()
{
KillTween();
m_ScaleTransform.localScale = mStartScale;
}
public void OnPointerDown(PointerEventData eventData)
{
if (mIsPress || !mIsEnable || !mInteractable) return;
mIsPress = true;
DownAnim();
}
public void OnPointerUp(PointerEventData eventData)
{
if (!mIsPress || !mIsEnable) return;
mIsPress = false;
if (mIsStay) UpAnim();
}
public void OnPointerEnter(PointerEventData eventData)
{
if (!mIsEnable || !mInteractable) return;
mIsStay = true;
if (mIsPress) DownAnim();
}
public void OnPointerExit(PointerEventData eventData)
{
if (!mIsPress || !mIsEnable || !mIsStay) return;
mIsStay = false;
UpAnim();
}
public void DownAnim()
{
AnimInternal(DownSetting);
}
public void UpAnim()
{
AnimInternal(UpSetting);
if (NeedSecondUpSetting)
mTween.OnComplete(() => { AnimInternal(SecondUpSetting); });
}
private void AnimInternal(TweenSetting setting)
{
KillTween();
mTween = m_ScaleTransform.DOScale(RelativeScale ? Vector3.Scale(setting.Scale, mStartScale) : setting.Scale, setting.Duration).SetUpdate(true);
if (setting.UseCurve)
mTween.SetEase(setting.Curve);
else
mTween.SetEase(setting.EaseType);
}
private void KillTween()
{
mTween?.Kill();
mTween = null;
}
}
}