Skip to content

Instantly share code, notes, and snippets.

@webcoyote
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webcoyote/a0e11799a91a2d967083 to your computer and use it in GitHub Desktop.
Save webcoyote/a0e11799a91a2d967083 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e # crash on errors
set -u # crash on undefined variables
set -o pipefail # crash when intermediate program in pipe fails
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mcs "$SCRIPT_DIR/timer.cs" "$@"
using UnityEngine;
using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
public class Main : MonoBehaviour {
private class TimeFunc {
public TimeFunc (string name, Func<float> time, bool applyDelta = true) {
Name = name;
Time = time;
ApplyDelta = applyDelta;
LastValue = time();
}
public float Delta () {
var curr = Time();
if (!ApplyDelta)
return curr;
var delta = curr - LastValue;
LastValue = curr;
return delta;
}
public readonly string Name;
private readonly Func<float> Time;
private readonly bool ApplyDelta;
private float LastValue;
};
private TimeFunc[] m_times;
void Awake () {
m_times = new TimeFunc[] {
// Time functions
new TimeFunc("tick", () => Environment.TickCount / 1000f),
new TimeFunc("stop", () => Stopwatch.GetTimestamp() / 1000f / 10000f),
new TimeFunc("real", () => Time.realtimeSinceStartup),
new TimeFunc("time", () => Time.time),
new TimeFunc("levl", () => Time.timeSinceLevelLoad),
new TimeFunc("fixd", () => Time.fixedTime),
new TimeFunc("unsc", () => Time.unscaledTime),
// Value functions
new TimeFunc("*scale", () => Time.timeScale, false),
new TimeFunc("*delta", () => Time.deltaTime * 1000f, false),
new TimeFunc("*smoot", () => Time.smoothDeltaTime * 1000f, false),
new TimeFunc("*unsca", () => Time.unscaledDeltaTime * 1000f, false),
};
Application.targetFrameRate = TargetFrameRate;
m_frameCount = 0;
}
void Update () {
if (++m_frameCount < TargetFrameRate)
return;
m_frameCount = 0;
var sb = new StringBuilder(256);
sb.Append(DateTime.UtcNow.ToLongTimeString());
foreach (var time in m_times)
sb.AppendFormat(" {0}:{1:0.0}", time.Name, time.Delta());
UnityEngine.Debug.Log(sb.ToString());
}
const int TargetFrameRate = 60;
int m_frameCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment