-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDown.cs
More file actions
155 lines (129 loc) · 4.17 KB
/
Copy pathCountDown.cs
File metadata and controls
155 lines (129 loc) · 4.17 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* USAGE:
GameObject countDown;
CountDown scriptCountDown;
void Start(){
scriptCountDown = countDown.GetComponent<CountDown>();
}
void Update(){
if (countDown.activeSelf)
{
scriptCountDown.startCountDown();
}
}
*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CountDown : MonoBehaviour {
private enum countDowndStates {WAITING, FINISHED, INPROGRESS};
private countDowndStates state;
private Text textUI;
private int actualNumber = 3;
private int sizeFont = 300;
private int actualSizeFont = 300;
private string txtAdelante;
public string TxtAdelante {
get { return txtAdelante; }
set { txtAdelante = value; }
}
void Start()
{
textUI = GetComponent<Text>();
state = countDowndStates.WAITING;
}
IEnumerator showLastMessage()
{
while (actualNumber > 0)
{
textUI.fontSize = actualSizeFont;
textUI.text = actualNumber.ToString();
if (actualSizeFont <= 0)
{
actualNumber--;
actualSizeFont = sizeFont;
}
else
{
actualSizeFont -= 5;
}
yield return new WaitForSeconds(3.0f);
}
textUI.fontSize = 100;
textUI.text = txtAdelante;
yield return new WaitForSeconds(3.0f);
finish();
}
private void finish()
{
state = countDowndStates.FINISHED;
gameObject.SetActive(false);
}
public void startCountDown()
{
if (state == countDowndStates.WAITING)
{
state = countDowndStates.INPROGRESS;
}
StartCoroutine(showLastMessage());
}
public bool isFinished()
{
return state == countDowndStates.FINISHED;
}
}
############################################################
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using TMPro;
public class Countdown : MonoBehaviour
{
[Header("Settings")]
[Tooltip("Set true to start counting (On Start event will be triggered the first time it is set to true)")]
[SerializeField] private bool _isCounting = true; // Is currently counting or not
[Tooltip("Cooldown duration")]
[SerializeField] private float _duration = 10; // Duration of the cooldown
[Header("GUI")]
[Tooltip("Text that can show the countdown")]
[SerializeField] private TextMeshProUGUI _countdownText = null; // Display text
[Header("Countdown Events")]
[Tooltip("Event that is triggered when countdown is started")]
[SerializeField] private UnityEvent _onStart = null; // Event that is fired when countdown starts
[Tooltip("Event that is triggered when countdown is finished")]
[SerializeField] private UnityEvent _onFinish = null; // Event that is fired when countdown is over
private float _counter = 0; // Timer which counts for countdown
private bool _isFinised = false; // Finish state
private bool _isStart = false; // Start state
// =============================== GETTER/SETTERS ===================================
public bool IsCounting
{
get{ return _isCounting; }
set{ _isCounting = value; }
}
// ================================== MONOBEHAVIOUR ====================================
private void Start()
{
_counter = _duration; // Set timer
_isStart = false; // Countdown is not started
}
private void Update()
{
// Check if timer can count
if(_isCounting == true && _isFinised == false)
{
if(_isStart == false)
{
_isStart = true;
_onStart.Invoke();
}
_counter -= Time.deltaTime;
if(_countdownText) _countdownText.text = ((int)Mathf.Ceil(_counter)).ToString();
if(_counter <= 0)
{
_isFinised = true;
_onFinish.Invoke();
}
}
}
}