How do I do a thousandths of a second readout, as a timer, without allocating any garbage?
I understand that it's possible to build an array of strings, from 000 to 999 for the 0.999, and get the appropriate string of these for each/any thousandths of a second scenario, from the thousandthsArray[nnn]
However, how do I divide it out from each time report, each frame, without creating garbage in the process of dividing out the seconds.
eg. the current time is 14.397
I need to get at and separate out that 397 from the 14 seconds without creating any garbage.
But the maths functions seem to create a little garbage, each and every frame. And nothing I can think of is particularly optimal. Or even working.
I don't think this is possible without creating any garbage. You should go for as little as possible.
You could use Mathf.Floor
which rounds it down to the last int
making it "forget" any decimals and then subtract it from the original value like
float time = 14.6978f;
float fullSeconds = Mathf.Floor(time);
// = 14 (rounded down)
int thousandthOfSeconds = Mathf.RoundToInt((time - fullSeconds) * 1000);
// = 698 (rounded up)
that's at least way cheaper than dealing with any strings there.