How to get Character Count From TMPro Object

How do you get the number of characters in a text mesh pro object? I have a script that creates a bunch of textMeshPro objects, adds them to a list, and aligns them depending on their number in the list. But I would also like to space them apart depending on how many characters are in the string, here’s my script currently,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System.Linq;

public class TextWheelFlooper : MonoBehaviour
{
    public List<string> texts = new List<string>(1);
    public List<TextMeshProUGUI> textPros;
    private int numberCount = 0;
    public Transform textWheelPosition;
    public GameObject textObj;

    void Start()
    {
        textWheelPosition = this.transform;

        for (int i = 0; i < texts.Count; i++)
        {
            textPros.Add(Instantiate(textObj.GetComponent<TextMeshProUGUI>(),transform.position, Quaternion.identity, transform));
        }
        for (int i = 0; i < textPros.Count; i++)
        {
            textPros[i].transform.position = new Vector3(transform.position.x + i * textPros[i] * 10, transform.position.y, transform.position.z);
        }
        textPros[numberCount].transform.position = new Vector3(transform.position.x, transform.position.y + 10, transform.position.z);

        for (int i = 0; i < texts.Count; i++)
        {
            textPros[i].text = texts[i];
        }
    }

public void NumberFlipUp()
    {

    }
}

obviously the + textPros is where I will be putting the character count… But how do I retrieve that? I’m trying to make a basic wheel that you can cycle through and whichever one is selected will be in the center, but in it’s current state, if the string is too long, they will overlap.
Thanks in advance.

Is that different from just the length of the string you are putting in the TextMeshProUGUI? C# strings have a .length

I tried * text.Length, but it didn’t work, it just put them each 80 units apart.

Edit: with varying lengths of strings

Okay, I figured it out, I should’ve been checking the string’s lengths instead of the TMPro’s length, but for those interested, to get a TMPro objects length, it’s TMPro.textinfo.characterCount. I also moved my second for loop to the bottom

Here’s what I changed my code to, a somewhat convoluted math equation, but it get’s the job done, even with overly long strings, hope it’s of some use to someone.

    void Start()
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;


public class TextWheelFlooper : MonoBehaviour
{
    public List<string> texts = new List<string>(1);
    public List<TextMeshProUGUI> textPros;
    private int numberCount = 0;
    public Transform textWheelPosition;
    public GameObject textObj;
    private void Awake()
    {
      //  textPros = new List<TextMeshProUGUI>(texts.Count);
    }
    void Start()
    {
        textWheelPosition = this.transform;
        float previousPosX;
        for (int i = 0; i < texts.Count; i++)
        {
            textPros.Add(Instantiate(textObj.GetComponent<TextMeshProUGUI>(),transform.position, Quaternion.identity, transform));
        }
        for (int i = 0; i < texts.Count; i++)
        {
            textPros[i].text = texts[i];
        }
        for (int i = 0; i < textPros.Count; i++)
        {
            previousPosX = textPros[i].transform.position.x * ((float)i / 2) * 1.25f;
            print(textPros[i].textInfo.characterCount);
            textPros[i].transform.position = new Vector3(transform.position.x + previousPosX + texts[i].Length * 2, transform.position.y, transform.position.z);
        }
        textPros[numberCount].transform.position = new Vector3(transform.position.x, transform.position.y + 10, transform.position.z);
    }
public void NumberFlipUp()
    {
    }
}
1 Like