Tuesday, January 16, 2018

Here's a little utility script for Unity. Since I made it I've used it a few times in my prototype, so thought I'd share.

It's basically a way to get the time since something happened, made to be a member of a class/component. It's so simple that I'm guessing it already exists in a form that I've missed.

using UnityEngine; public struct TimeSince { float time; public static implicit operator float(TimeSince ts) { return Time.time - ts.time; } public static implicit operator TimeSince(float ts) { return new TimeSince { time = Time.time - ts }; } }

It'll act like a float, but it'll change over time.

TimeSince ts; void Start() { ts = 0; } void Update() { if ( ts > 10 ) { DoSomethingAfterTenSeconds(); } }

It's a struct so it doesn't create garbage.

Turn your phone to view blog list