// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik // using System; using System.Diagnostics; namespace Animancer { /// A very simple timer system based on a . public struct SimpleTimer : IDisposable { /************************************************************************************************************************/ /// The system used to track time. public static readonly Stopwatch Stopwatch = Stopwatch.StartNew(); /// The amount of time that has passed (in seconds) since the first timer was started. public static double CurrentTime => Stopwatch.Elapsed.TotalSeconds; /************************************************************************************************************************/ /// An optional prefix for . public string name; /// The from when this timer instance was started. public double startTime; /// The total amount of time this timer instance has been running (in seconds). public double total; /// The number format used by . const string Format = "0.000"; /************************************************************************************************************************/ /// Has been called and not? public bool IsStarted => startTime != 0; /************************************************************************************************************************/ /// Creates a new with the specified `name`. /// /// You will need to call to start the timer. Or use the static /// /// public SimpleTimer(string name) { this.name = name; startTime = 0; total = 0; } /************************************************************************************************************************/ /// Creates a new with the specified `name` and starts it. public static SimpleTimer Start(string name = null) => new SimpleTimer { name = name, startTime = CurrentTime, }; /************************************************************************************************************************/ /// /// Stores the in so that will be able to /// calculate how much time has passed. /// /// Does nothing if the was already set. public bool Start() { if (startTime != 0) return false; startTime = CurrentTime; return true; } /************************************************************************************************************************/ /// /// Adds the amount of time that has passed since the to the and /// clears the . /// /// Does nothing if the was already cleared (or not set). public bool Stop() { if (startTime == 0) return false; var endTime = CurrentTime; total += endTime - startTime; startTime = 0; return true; } /************************************************************************************************************************/ /// Calls and returns a string representation of the . public override string ToString() { Stop(); return string.IsNullOrEmpty(name) ? total.ToString(Format) : $"{name}: {total.ToString(Format)}"; } /************************************************************************************************************************/ /// Calls and logs the result. public void Dispose() { var text = ToString(); UnityEngine.Debug.Log(text); } /************************************************************************************************************************/ } }