118 lines
3.0 KiB
C#
118 lines
3.0 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class TimeController : MonoBehaviour
|
||
|
|
{
|
||
|
|
public TMP_Text TODIndicator;
|
||
|
|
public Slider TODSlider;
|
||
|
|
[HideInInspector]
|
||
|
|
public GameObject sun;
|
||
|
|
[HideInInspector]
|
||
|
|
public Light sunLight;
|
||
|
|
|
||
|
|
[Range(0, 24)]
|
||
|
|
public float timeOfDay = 12;
|
||
|
|
|
||
|
|
public float secondsPerMinute = 60;
|
||
|
|
[HideInInspector]
|
||
|
|
public float secondsPerHour;
|
||
|
|
[HideInInspector]
|
||
|
|
public float secondsPerDay;
|
||
|
|
|
||
|
|
// Realistic - 1
|
||
|
|
// Normal - 2400
|
||
|
|
public float timeMultiplier = 2400;
|
||
|
|
|
||
|
|
public static string PublicFormattedTimeOfDay = "00:00";
|
||
|
|
|
||
|
|
[ShowOnly]
|
||
|
|
public string StringTimeOfDay = "00:00";
|
||
|
|
public static bool MilitaryTime = false;
|
||
|
|
public float MinutesBy = 60;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
sun = gameObject;
|
||
|
|
sunLight = gameObject.GetComponent<Light>();
|
||
|
|
|
||
|
|
secondsPerHour = secondsPerMinute * 60;
|
||
|
|
secondsPerDay = secondsPerHour * 24;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
SunUpdate();
|
||
|
|
|
||
|
|
timeOfDay += (Time.deltaTime / secondsPerDay) * timeMultiplier;
|
||
|
|
StringTimeOfDay = FormatTime(timeOfDay, MilitaryTime);
|
||
|
|
PublicFormattedTimeOfDay = StringTimeOfDay;
|
||
|
|
|
||
|
|
if (timeOfDay >= 24)
|
||
|
|
timeOfDay = 0;
|
||
|
|
|
||
|
|
|
||
|
|
if (TODIndicator)
|
||
|
|
TODIndicator.text = StringTimeOfDay;
|
||
|
|
if (TODSlider)
|
||
|
|
{
|
||
|
|
TODSlider.onValueChanged.RemoveAllListeners();
|
||
|
|
TODSlider.onValueChanged.AddListener(delegate { UpdateTime(TODSlider.value); });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void UpdateTime(Single ResultTime)
|
||
|
|
{
|
||
|
|
if (TODSlider)
|
||
|
|
timeOfDay = ResultTime;
|
||
|
|
}
|
||
|
|
public string FormatTime(float TimeOfDay, bool military = true)
|
||
|
|
{
|
||
|
|
TimeSpan timeSpan = TimeSpan.FromHours(TimeOfDay);
|
||
|
|
string hours = timeSpan.Hours.ToString();
|
||
|
|
string minutes = timeSpan.Minutes.ToString();
|
||
|
|
|
||
|
|
if (minutes == "0")
|
||
|
|
minutes += "0";
|
||
|
|
if (timeSpan.Minutes <= 9)
|
||
|
|
minutes = "0" + timeSpan.Minutes;
|
||
|
|
if (timeSpan.Hours < 10 && military)
|
||
|
|
{
|
||
|
|
if (hours == "0")
|
||
|
|
hours += "0";
|
||
|
|
else
|
||
|
|
hours = "0" + hours;
|
||
|
|
}
|
||
|
|
if (!military)
|
||
|
|
{
|
||
|
|
var IntHours = int.Parse(hours);
|
||
|
|
|
||
|
|
if (IntHours <= 12)
|
||
|
|
{
|
||
|
|
if (hours == "0")
|
||
|
|
hours = "12";
|
||
|
|
|
||
|
|
minutes += " AM";
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
minutes += " PM";
|
||
|
|
hours = (IntHours - 12).ToString();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
var output = String.Format("{0}:{1}", hours, minutes);
|
||
|
|
return output;
|
||
|
|
}
|
||
|
|
public void SunUpdate()
|
||
|
|
{
|
||
|
|
if (TODSlider)
|
||
|
|
TODSlider.value = timeOfDay;
|
||
|
|
sun.transform.localRotation = Quaternion.Euler(((timeOfDay / 24) * 360f) - 90, 90, 0);
|
||
|
|
}
|
||
|
|
}
|