Created
February 18, 2018 00:13
-
-
Save reidblomquist/feac3a830fc9c092aca35ae19a700c40 to your computer and use it in GitHub Desktop.
example stopwatch solution (taking catlikecoding's clock tutorial further)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Clock : MonoBehaviour { | |
const float | |
degreesPerHour = 30f, | |
degreesPerMinute = 6f, | |
degreesPerSecond = 6f; | |
TimeSpan stopwatchStartCont; | |
DateTime stopwatchStartDisc; | |
public bool stopwatch = false; | |
public bool stopwatchRunning = false; | |
public Text digitalText; | |
public Transform hoursTransform, minutesTransform, secondsTransform; | |
public bool continuous; | |
void Update () { | |
HandleInput(); | |
if (continuous) { | |
UpdateContinuous(); | |
} | |
else { | |
UpdateDiscrete(); | |
} | |
} | |
void HandleInput () | |
{ | |
if (Input.GetKeyDown("space")) | |
{ | |
if (!stopwatch && !stopwatchRunning) | |
{ | |
stopwatch = true; | |
ResetStopwatch(); | |
} | |
else if (stopwatch && !stopwatchRunning) | |
{ | |
stopwatchRunning = true; | |
stopwatchStartCont = DateTime.Now.TimeOfDay; | |
stopwatchStartDisc = DateTime.Now; | |
} | |
else if (stopwatch && stopwatchRunning) | |
{ | |
stopwatchRunning = false; | |
ResetStopwatch(); | |
} | |
} | |
if (Input.GetKeyDown("escape")) | |
{ | |
stopwatch = false; | |
stopwatchRunning = false; | |
} | |
if (Input.GetKeyDown("c")) | |
{ | |
continuous = !continuous; | |
} | |
} | |
void ResetStopwatch () | |
{ | |
UpdateDigital(0, 0, 0); | |
hoursTransform.localRotation = | |
Quaternion.Euler(0f, 0f, 0f); | |
minutesTransform.localRotation = | |
Quaternion.Euler(0f, 0f, 0f); | |
secondsTransform.localRotation = | |
Quaternion.Euler(0f, 0f, 0f); | |
} | |
void UpdateDigital (int hour, int minute, int second) | |
{ | |
digitalText.text = hour.ToString().PadLeft(2, '0') + ":" + minute.ToString().PadLeft(2, '0') + ":" + second.ToString().PadLeft(2, '0'); | |
} | |
void UpdateContinuous () { | |
TimeSpan time = DateTime.Now.TimeOfDay; | |
if (!stopwatch) | |
{ | |
float hour = (float)time.TotalHours; | |
float minute = (float)time.TotalMinutes; | |
float second = (float)time.TotalSeconds; | |
hoursTransform.localRotation = | |
Quaternion.Euler(0f, hour * degreesPerHour, 0f); | |
minutesTransform.localRotation = | |
Quaternion.Euler(0f, minute * degreesPerMinute, 0f); | |
secondsTransform.localRotation = | |
Quaternion.Euler(0f, second * degreesPerSecond, 0f); | |
UpdateDigital((int)time.Hours, (int)time.Minutes, (int)time.Seconds); | |
} | |
if (stopwatchRunning) | |
{ | |
TimeSpan digitalTime = time - stopwatchStartCont; | |
float stopHour = (float)time.TotalHours - (float)stopwatchStartCont.TotalHours; | |
float stopMinute = (float)time.TotalMinutes - (float)stopwatchStartCont.TotalMinutes; | |
float stopSecond = (float)time.TotalSeconds - (float)stopwatchStartCont.TotalSeconds; | |
hoursTransform.localRotation = | |
Quaternion.Euler(0f, stopHour * degreesPerHour, 0f); | |
minutesTransform.localRotation = | |
Quaternion.Euler(0f, stopMinute * degreesPerMinute, 0f); | |
secondsTransform.localRotation = | |
Quaternion.Euler(0f, stopSecond * degreesPerSecond, 0f); | |
UpdateDigital((int)digitalTime.TotalHours, (int)digitalTime.Minutes, (int)digitalTime.Seconds); | |
} | |
} | |
void UpdateDiscrete () { | |
DateTime time = DateTime.Now; | |
if (!stopwatch) | |
{ | |
hoursTransform.localRotation = | |
Quaternion.Euler(0f, time.Hour * degreesPerHour, 0f); | |
minutesTransform.localRotation = | |
Quaternion.Euler(0f, time.Minute * degreesPerMinute, 0f); | |
secondsTransform.localRotation = | |
Quaternion.Euler(0f, time.Second * degreesPerSecond, 0f); | |
UpdateDigital(time.Hour, time.Minute, time.Second); | |
} | |
if (stopwatchRunning) | |
{ | |
TimeSpan stopTime = time - stopwatchStartDisc; | |
hoursTransform.localRotation = | |
Quaternion.Euler(0f, stopTime.Hours * degreesPerHour, 0f); | |
minutesTransform.localRotation = | |
Quaternion.Euler(0f, stopTime.Minutes * degreesPerMinute, 0f); | |
secondsTransform.localRotation = | |
Quaternion.Euler(0f, stopTime.Seconds * degreesPerSecond, 0f); | |
UpdateDigital((int)stopTime.TotalHours, stopTime.Minutes, stopTime.Seconds); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment