Skip to content

Instantly share code, notes, and snippets.

@nikescar1
Created October 18, 2021 21:20
Show Gist options
  • Save nikescar1/3ca1fbef92573551b26288b1bcf0d955 to your computer and use it in GitHub Desktop.
Save nikescar1/3ca1fbef92573551b26288b1bcf0d955 to your computer and use it in GitHub Desktop.
Example of setting TextMeshPro text every frame without allocations
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class NumberStringsTesting : MonoBehaviour
{
public List<TMP_Text> digits; //Add these in reverse order (Highest digit first, lowest digit last)
List<int> numbers = new List<int>(50);
void Start()
{
for (int i = 0; i < 50; i++)
{
numbers.Add(0);
}
}
void Update()
{
int num = (int)Time.realtimeSinceStartup;
int index = 0;
while (num > 0)
{
numbers[index] = num % 10;
num = num / 10;
index++;
}
for (int i = 0; i < index + 1; i++)
{
digits[i].SetText(NumberStrings.Zero_To_Nine_Strings[numbers[i]]);
}
}
}
public class NumberStrings
{
public static string[] Zero_To_Nine_Strings = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public static char[] Zero_To_Nine_Chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment