Simple script to add allocation free text timers to your games, for example level timer or in-game clock.
- HH:MM:SS.FFF
- MM:SS.FFF
- SS:FFF
- HH:MM:SS
- MM:SS
- SS
Strings are immutable and every time when we need to update .text property we have to allocate new string. This script uses TMP_Text.SetCharArray() to avoid unnecessary allocations.
The editor will still allocate memory, because TextMeshPro will generate a new string to update the inspector.
public void SetCharArray(char[] sourceText, int start, int length)
{
PopulateTextBackingArray(sourceText, start, length);
m_IsTextBackingStringDirty = true;
#if UNITY_EDITOR
m_text = InternalTextBackingArrayToString(); // Allocation happens here
#endif
// Set input source
m_inputSource = TextInputSources.SetTextArray;
PopulateTextProcessingArray();
m_havePropertiesChanged = true;
SetVerticesDirty();
SetLayoutDirty();
}Just copy TimerText.cs somewhere to your project. For example here: Assets/Scripts/TimerText.cs
Basically you have two ways to update text value
by passing total seconds:
void SetSeconds(float totalSeconds);or TimeSpan:
void SetTimespan(TimeSpan timeSpan)