Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

About

Simple script to add allocation free text timers to your games, for example level timer or in-game clock.

Supported formats:

  • HH:MM:SS.FFF
  • MM:SS.FFF
  • SS:FFF
  • HH:MM:SS
  • MM:SS
  • SS

Why

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.

NOTE

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();
}

Installation

Just copy TimerText.cs somewhere to your project. For example here: Assets/Scripts/TimerText.cs

Usage

Basically you have two ways to update text value

by passing total seconds:

void SetSeconds(float totalSeconds);

or TimeSpan:

void SetTimespan(TimeSpan timeSpan)

About

Allocation free TextMeshPro timer for Unity

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages