Tanoda
CooldownButton.cs
Go to the documentation of this file.
1
3
6
8{
9 [AddComponentMenu("UI/Extensions/Cooldown Button")]
10 public class CooldownButton : MonoBehaviour, IPointerDownHandler
11 {
12 #region Sub-Classes
13 [System.Serializable]
14 public class CooldownButtonEvent : UnityEvent<PointerEventData.InputButton> { }
15 #endregion
16
17 #region Private variables
18
19 [SerializeField]
20 private float cooldownTimeout;
21 [SerializeField]
22 private float cooldownSpeed = 1;
23 [SerializeField][ReadOnly]
24 private bool cooldownActive;
25 [SerializeField][ReadOnly]
26 private bool cooldownInEffect;
27 [SerializeField][ReadOnly]
28 private float cooldownTimeElapsed;
29 [SerializeField][ReadOnly]
30 private float cooldownTimeRemaining;
31 [SerializeField][ReadOnly]
32 private int cooldownPercentRemaining;
33 [SerializeField][ReadOnly]
34 private int cooldownPercentComplete;
35
36 PointerEventData buttonSource;
37 #endregion
38
39 #region Public Properties
40
41 public float CooldownTimeout
42 {
43 get { return cooldownTimeout; }
44 set { cooldownTimeout = value; }
45 }
46
47 public float CooldownSpeed
48 {
49 get { return cooldownSpeed; }
50 set { cooldownSpeed = value; }
51 }
52
53 public bool CooldownInEffect
54 {
55 get { return cooldownInEffect; }
56 }
57
58 public bool CooldownActive
59 {
60 get { return cooldownActive; }
61 set { cooldownActive = value; }
62 }
63
65 {
66 get { return cooldownTimeElapsed; }
67 set { cooldownTimeElapsed = value; }
68 }
69
71 {
72 get { return cooldownTimeRemaining; }
73 }
74
76 {
77 get { return cooldownPercentRemaining; }
78 }
79
81 {
82 get { return cooldownPercentComplete; }
83 }
84
85 #endregion
86
87 #region Events
88 [Tooltip("Event that fires when a button is initially pressed down")]
90 [Tooltip("Event that fires when a button is released")]
92 [Tooltip("Event that continually fires while a button is held down")]
94 #endregion
95
96 #region Update
97
98 // Update is called once per frame
99 void Update()
100 {
101 if (CooldownActive)
102 {
103 cooldownTimeRemaining -= Time.deltaTime * cooldownSpeed;
104 cooldownTimeElapsed = CooldownTimeout - CooldownTimeRemaining;
105 if (cooldownTimeRemaining < 0)
106 {
107 StopCooldown();
108 }
109 else
110 {
111 cooldownPercentRemaining = (int)(100 * cooldownTimeRemaining * CooldownTimeout / 100);
112 cooldownPercentComplete = (int)((CooldownTimeout - cooldownTimeRemaining) / CooldownTimeout * 100);
113 }
114 }
115 }
116 #endregion
117
118 #region Public Methods
119
123 public void PauseCooldown()
124 {
126 {
127 CooldownActive = false;
128 }
129 }
130
134 public void RestartCooldown()
135 {
137 {
138 CooldownActive = true;
139 }
140 }
141
145 public void StartCooldown()
146 {
147 PointerEventData emptySource = new PointerEventData(EventSystem.current);
148 buttonSource = emptySource;
149 OnCooldownStart.Invoke(emptySource.button);
150 cooldownTimeRemaining = cooldownTimeout;
151 CooldownActive = cooldownInEffect = true;
152 }
153
157 public void StopCooldown()
158 {
159 cooldownTimeElapsed = CooldownTimeout;
160 cooldownTimeRemaining = 0;
161 cooldownPercentRemaining = 0;
162 cooldownPercentComplete = 100;
163 cooldownActive = cooldownInEffect = false;
164 if (OnCoolDownFinish != null) OnCoolDownFinish.Invoke(buttonSource.button);
165 }
166
170 public void CancelCooldown()
171 {
172 cooldownActive = cooldownInEffect = false;
173 }
174
175 #endregion
176
177 #region IPointerDownHandler
178
179 void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
180 {
181 buttonSource = eventData;
183 {
184 if (OnButtonClickDuringCooldown != null) OnButtonClickDuringCooldown.Invoke(eventData.button);
185 }
186 if (!CooldownInEffect)
187 {
188 if(OnCooldownStart != null) OnCooldownStart.Invoke(eventData.button);
189 cooldownTimeRemaining = cooldownTimeout;
190 cooldownActive = cooldownInEffect = true;
191 }
192 }
193
194 #endregion
195
196 }
197}
void CancelCooldown()
Stop a running Cooldown and retain current values
void PauseCooldown()
Pause Cooldown without resetting values, allows Restarting of cooldown
void StopCooldown()
Stop a running Cooldown and reset all values
void StartCooldown()
Start a cooldown from outside
CooldownButtonEvent OnButtonClickDuringCooldown
void RestartCooldown()
Restart a paused cooldown
Credit Erdener Gonenc - @PixelEnvision.