namespace UIWidgets
{
using System;
using UnityEngine;
///
/// Time functions.
///
public static class UtilitiesTime
{
///
/// Function to get time to use with animations.
/// Can be replaced with custom function.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "Required.")]
public static Func GetTime = DefaultGetTime;
///
/// Function to get delta time to use with animations.
/// Can be replaced with custom function.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "Required.")]
public static Func GetDeltaTime = DefaultGetDeltaTime;
///
/// Function to get frame count.
/// Can be replaced with custom function.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "Required.")]
public static Func GetFrameCount = DefaultGetFrameCount;
///
/// Suspends the coroutine execution for the given amount of seconds.
/// Can be replaced with custom function.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "Required.")]
public static Func Wait = DefaultWait;
#if UNITY_2019_3_OR_NEWER
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Init()
{
GetTime = DefaultGetTime;
GetDeltaTime = DefaultGetDeltaTime;
GetFrameCount = DefaultGetFrameCount;
Wait = DefaultWait;
}
#endif
///
/// Default GetFrameCount function from the default Time class.
///
/// Frame count.
public static int DefaultGetFrameCount()
{
return Time.frameCount;
}
///
/// Default GetTime function from the default Time class.
///
/// Return unscaled time.
/// Time.
public static float DefaultGetTime(bool unscaledTime)
{
return unscaledTime ? Time.unscaledTime : Time.time;
}
///
/// Suspends the coroutine execution for the given amount of seconds.
///
/// Seconds.
/// Use unscaled time.
/// Coroutine.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0401:Possible allocation of reference type enumerator", Justification = "Enumerator is reusable.")]
public static CustomYieldInstruction DefaultWait(float seconds, bool unscaledTime = false)
{
return WaitForSecondsCustom.Instance(seconds, unscaledTime);
}
///
/// Default GetUnscaledTime function.
///
/// Default Time.unscaledTime.
public static float DefaultGetUnscaledTime()
{
return Time.unscaledTime;
}
///
/// Default GetDeltaTime function from the default Time class.
///
/// Return unscaled delta time.
/// Delta Time.
public static float DefaultGetDeltaTime(bool unscaledTime)
{
return unscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
}
///
/// Check how much time takes to run specified action.
///
/// Action.
/// Text to add to log.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0101:Array allocation for params parameter", Justification = "Required.")]
public static void CheckRunTime(Action action, string log = "")
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
action();
sw.Stop();
var ts = sw.Elapsed;
string result = string.Format(
"RunTime {0}:{1}:{2}.{3}; {4}",
ts.Hours.ToString("00"),
ts.Minutes.ToString("00"),
ts.Seconds.ToString("00"),
(ts.Milliseconds / 10).ToString("0000"),
log);
Debug.Log(result);
}
}
}