Last active
June 9, 2023 23:30
-
-
Save restush/a63a34a710ec6945ab435bd81bb3186c to your computer and use it in GitHub Desktop.
Simple serializable System.DateTime and Time Machine for Unity Engine.
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; | |
/// <summary> | |
/// Simple Date Time based on <see cref="System.DateTime"/>. | |
/// <example> | |
/// <br/><br/> | |
/// Usage 1: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(DateTime.Now); | |
/// </code> | |
/// <br/><br/> | |
/// Usage 2: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(new DateTime(1996,12,28)); | |
/// </code> | |
/// </example> | |
/// </summary> | |
[System.Serializable] | |
public class SimpleDateTime | |
{ | |
[SerializeField] private long _ticks; | |
[SerializeField] private int _hour; | |
[SerializeField] private int _minute; | |
[SerializeField] private int _second; | |
[SerializeField] private int _milisecond; | |
[SerializeField] private DayOfWeek _dayOfWeek; | |
[SerializeField] private int _day; | |
[SerializeField] private int _month; | |
[SerializeField] private int _year; | |
public SimpleDateTime(DateTime dateTime) | |
{ | |
Ticks = dateTime.Ticks; | |
} | |
/// <summary> | |
/// Gets the number of ticks that represent the date and time of this instance. | |
/// </summary> | |
public virtual long Ticks { get => _ticks; | |
private set | |
{ | |
_ticks = value; | |
var date = new DateTime(value); | |
_hour = date.Hour; | |
_minute = date.Minute; | |
_second = date.Second; | |
_milisecond = date.Millisecond; | |
_dayOfWeek = date.DayOfWeek; ; | |
_day = date.Day; | |
_month = date.Month; | |
_year = date.Year; | |
} | |
} | |
public virtual int Hour { get => _hour; } | |
public virtual int Minute { get => _minute;} | |
public virtual int Second { get => _second; } | |
public virtual int Milisecond { get => _milisecond; } | |
public virtual DayOfWeek DayOfWeek { get => _dayOfWeek; } | |
public virtual int Day { get => _day; } | |
public virtual int Month { get => _month; } | |
public virtual int Year { get => _year; } | |
public virtual DateTime DateTime { get => new(_ticks); } | |
/// <summary> | |
/// Add Milisecond to instance. | |
/// <example> | |
/// <br/><br/>Usage 1: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(DateTime.Now); | |
/// simpleDateTime.AddMilisecond(500); | |
/// </code> | |
/// Usage 2: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(DateTime.Now); | |
/// float timeDeltaToMilisecond = Time.deltaTime * 1000; | |
/// simpleDateTime.AddMilisecond(timeDeltaToMilisecond); | |
/// </code> | |
/// </example> | |
/// </summary> | |
/// <param name="value">Milisecond</param> | |
public virtual void AddMiliseconds(float value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime += TimeSpan.FromMilliseconds(value); | |
Ticks = _dateTime.Ticks; | |
} | |
/// <summary> | |
/// Add Seconds to instance. | |
/// <example> | |
/// <br/><br/>Usage 1: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(DateTime.Now); | |
/// simpleDateTime.AddSeconds(10); | |
/// </code> | |
/// Usage 2: | |
/// <code> | |
/// var simpleDateTime = new SimpleDateTime(DateTime.Now); | |
/// simpleDateTime.AddMilisecond(Time.deltaTime); | |
/// </code> | |
/// </example> | |
/// </summary> | |
/// <param name="value">Seconds</param> | |
public virtual void AddSeconds(float value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime += TimeSpan.FromSeconds(value); | |
Ticks = _dateTime.Ticks; | |
} | |
/// <summary> | |
/// Add Hours to instance. | |
/// </summary> | |
/// <param name="value">Hours</param> | |
public virtual void AddHours(float value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime += TimeSpan.FromHours(value); | |
Ticks = _dateTime.Ticks; | |
} | |
/// <summary> | |
/// Add Days to instance. | |
/// </summary> | |
/// <param name="value">Hours</param> | |
public virtual void AddDays(float value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime += TimeSpan.FromHours(value); | |
Ticks = _dateTime.Ticks; | |
} | |
/// <summary> | |
/// Add Months to instance | |
/// </summary> | |
/// <param name="value">Hours</param> | |
public virtual void AddMonths(int value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime = _dateTime.AddMonths(value); | |
Ticks = _dateTime.Ticks; | |
} | |
/// <summary> | |
/// Add Years to instance. | |
/// </summary> | |
/// <param name="value">Hours</param> | |
public virtual void AddYears(int value) | |
{ | |
var _dateTime = new DateTime(_ticks); | |
_dateTime = _dateTime.AddYears(value); | |
Ticks = _dateTime.Ticks; | |
} | |
public override string ToString() | |
{ | |
var _dateTime = new DateTime(_ticks); | |
return _dateTime.ToString(); | |
} | |
} |
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 UnityEngine; | |
public class TimeMachine : MonoBehaviour | |
{ | |
public static event System.Action<float, SimpleDateTime> OnTimeChanged; | |
[SerializeField] private int _timeSpeed = 1; | |
[SerializeField] private float _totalTime; | |
[SerializeField] private float _timeSinceStart; | |
[SerializeField] private SimpleDateTime _simpleDateTime; | |
private void Awake() | |
{ | |
_simpleDateTime = new SimpleDateTime(System.DateTime.Now); | |
} | |
private void Update() | |
{ | |
_timeSinceStart = Time.time; | |
var time = Time.deltaTime * _timeSpeed; | |
_totalTime += time; | |
_simpleDateTime.AddSeconds(time); | |
OnTimeChanged?.Invoke(_totalTime, _simpleDateTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment