0

I am new about unity,I found that in the function named SetCharArray, like this:

    public void SetCharArray(char[] sourceText, int start, int length)
    {
        PopulateTextBackingArray(sourceText, start, length);
        m_IsTextBackingStringDirty = true;
        m_text = InternalTextBackingArrayToString();
        m_inputSource = TextInputSources.SetTextArray;
        PopulateTextProcessingArray();
        m_havePropertiesChanged = true;
        SetVerticesDirty();
        SetLayoutDirty();
    }

there is a line with m_text = InternalTextBackingArrayToString();. The method InternalTextBackingArrayToString internally calls new string(array);, which can cause garbage collection issues due to memory allocation.

How can I avoid GC problems when using TextMeshPro Or am I misunderstanding something?

I tried a test like:

public class TestText : MonoBehaviour
{
    TextMeshProUGUI text;
    char[] chars = new char[12];

    static readonly char[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    void Start()
    {
        text = GetComponent<TextMeshProUGUI>();
    }
    // Update is called once per frame
    void Update()
    {
        chars[0] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[1] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[2] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[3] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[4] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[5] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[6] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[7] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[8] = numbers[UnityEngine.Random.Range(0, numbers.Length)];
        chars[9] = numbers[UnityEngine.Random.Range(0, numbers.Length)];

        text.SetCharArray(chars);
    }
}

the result is: enter image description here

New contributor
user25957020 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.