-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerControlUI.cs
More file actions
84 lines (72 loc) · 2.4 KB
/
TimerControlUI.cs
File metadata and controls
84 lines (72 loc) · 2.4 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
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace KongZEE.Timer
{
public class TimerControlUI : MonoBehaviour
{
[SerializeField] private float seconds = 1;
[SerializeField] GameObject _timerBox;
int index = 0;
Timer _timer;
Dictionary<int, TimerKey> timerKeys = new Dictionary<int, TimerKey>();
Dictionary<int, TextMeshProUGUI> timerTexts = new Dictionary<int, TextMeshProUGUI>();
public string SetSecondsString
{
get { return seconds.ToString(); }
set { seconds = float.Parse(value); }
}
public int Index
{
get { return index; }
set { index = value; }
}
public string IndexString
{
get { return index.ToString(); }
set { index = int.Parse(value); }
}
private void Start()
{
_timer = new Timer(this);
}
public void StartTimer()
{
GameObject timerGameObject = new GameObject($"Timer {seconds}");
TextMeshProUGUI timerText = timerGameObject.AddComponent<TextMeshProUGUI>();
timerGameObject.transform.SetParent(_timerBox.transform);
TimerKey timerKey = _timer.StartTimer(seconds, time =>
{
timerText.text = $"Timer {timerKeys.Count + 1} : {time}";
Debug.Log($"Timer {timerKeys.Count + 1} : {time}");
});
timerKeys.Add(timerKeys.Count, timerKey);
timerTexts.Add(timerTexts.Count, timerText);
}
public void Pause()
{
_timer.PauseTimer(timerKeys[index]);
}
public void Resume()
{
_timer.ResumeTimer(timerKeys[index]);
}
public void Stop()
{
Destroy(timerTexts[index].gameObject);
_timer.StopTimer(timerKeys[index]);
timerKeys.Remove(index);
timerTexts.Remove(index);
}
public void StopAll()
{
foreach (var timerText in timerTexts)
{
Destroy(timerText.Value.gameObject);
}
_timer.StopAllTimers();
timerKeys.Clear();
timerTexts.Clear();
}
}
}