/
Clock.cs
106 lines (94 loc) · 4.03 KB
/
Clock.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
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class Clock : MonoBehaviour
{
[Header("0,0,0 = SystemTime / else Custom Time")]
public Vector3 CurrentTime_Number;
[Header("Info Only")]
public float CurrentTime;
public string CurrentTime_String24H;
public string CurrentTime_String12H;
public enum DirectionOptions { Right, Left, Forward, Back }
[Header("Direction")]
public DirectionOptions ArrowDirection;
private bool _CustomTime;
private Vector3 _Direction;
private Vector3 _CurrentTime_NumberSmooth;
[Header("Clock")]
[SerializeField] private List<Clock_Arrow> Arrows = new List<Clock_Arrow>();
private void Start()
{
if (CurrentTime == 0 && CurrentTime_Number == Vector3.zero)
_CustomTime = false;
else
{
_CustomTime = true;
if (CurrentTime_Number != Vector3.zero)
CurrentTime = CurrentTime_Number.z + (CurrentTime_Number.y * 60) + (CurrentTime_Number.x * 3600);
}
switch (ArrowDirection)
{
case DirectionOptions.Right:
_Direction = Vector3.right;
break;
case DirectionOptions.Left:
_Direction = Vector3.left;
break;
case DirectionOptions.Forward:
_Direction = Vector3.forward;
break;
case DirectionOptions.Back:
_Direction = Vector3.back;
break;
}
}
void Update()
{
if (!_CustomTime)
CurrentTime = DateTime.Now.Second + (DateTime.Now.Minute * 60) + (DateTime.Now.Hour * 3600) + (DateTime.Now.Millisecond * 0.001f);
else
CurrentTime += 1 * Time.deltaTime;
CurrentTime_Number = new Vector3(Mathf.Floor(CurrentTime / 3600), Mathf.Floor((CurrentTime / 60) % 60), Mathf.Floor(CurrentTime % 60));
_CurrentTime_NumberSmooth = new Vector3(CurrentTime / 3600, (CurrentTime / 60) % 60, CurrentTime % 60);
CurrentTime_String24H = string.Format("{0:00}:{1:00}:{2:00}", Mathf.Floor(CurrentTime / 3600), Mathf.Floor((CurrentTime / 60) % 60), CurrentTime % 60);
CurrentTime_String12H = "";
if (CurrentTime > 43200)
CurrentTime_String12H = string.Format("{0:00}:{1:00} PM", Mathf.Floor(CurrentTime / 3600) - 12, Mathf.Floor((CurrentTime / 60) % 60));
else
CurrentTime_String12H = string.Format("{0:00}:{1:00} AM", Mathf.Floor(CurrentTime / 3600), Mathf.Floor((CurrentTime / 60) % 60));
for (int i = 0; i < Arrows.Count; i++)
{
switch (Arrows[i].Type)
{
case Clock_Arrow.ArrowType.Hour:
if (Arrows[i].Smooth)
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 12) * _CurrentTime_NumberSmooth.x, _Direction);
else
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 12) * CurrentTime_Number.x, _Direction);
break;
case Clock_Arrow.ArrowType.Minute:
if (Arrows[i].Smooth)
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 60) * _CurrentTime_NumberSmooth.y, _Direction);
else
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 60) * CurrentTime_Number.y, _Direction);
break;
case Clock_Arrow.ArrowType.Second:
if (Arrows[i].Smooth)
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 60) * _CurrentTime_NumberSmooth.z, _Direction);
else
Arrows[i].ArrowObj.rotation = Quaternion.AngleAxis((360 / 60) * CurrentTime_Number.z, _Direction);
break;
}
}
}
}
[System.Serializable]
public class Clock_Arrow
{
public enum ArrowType { Hour, Minute, Second }
public ArrowType Type;
public Transform ArrowObj;
public bool Smooth;
}