- Home /
Best Practices: TimeSpans
Hi there, this is, hopefully, a simple question. Why do I not see TimeSpans in tutorials/examples more? Are using these considered a bad practice? I know all sorts of nifty things can be done with just plain ol' floats and Time.deltaTime, but very little seems to fit the elegance (in my opinion) of something like:
timeInAir += TimeSpan.FromSeconds(Time.deltaTime);
I'm still new to Unity, but I'm afraid of building bad habits now if I should be staying away from TimeSpans/DateTimes. Are these just not used a lot because not everyone comes from a C# background and is used to these futuristic objects?
Specifically, I'm wondering if:
Are they performant (across all platforms)?
Do they work across all platforms?
Do they ever not work the way they should (from MS C# background) in Unity's custom .net compiler setup?
Sorry if this isn't considered constructive, but I think Best Practices are rather important to nail down early.
Thanks.
Answer by whydoidoit · Apr 15, 2013 at 08:55 AM
Um: timeInAir += Time.deltaTime - would give you the total time in the air in seconds!
I use TimeSpans but only when comparing or incrementing actual DateTime constructs.
They work on all platforms.
That wasn't the complete code! But,
TimeSpan timeInAir += TimeSpan.FromSeconds(Time.deltaTime);
doesn't make sense. And, well, doesn't actually compile either. It was an example of it, to me, making a little more sense than treating floats as time.
TimeSpan timeInAir; // private member
timeInAir += TimeSpan.FromSeconds(Time.deltaTime); // In update
(there...)
So, no reason not to use TimeSpans - just a lot of people don't?
Well they are slower than using floats - which are perfect for representing time in seconds within a game context. You can use TimeSpans if you like :)