ITextPreprocessor Usage

Hey all.

I’m trying to make use of ITextPreprocessor to handle custom text mesh effects (shaky text, etc). I was able to get the functionality I wanted (more or less) by just using an update function that checks for a text change on an attached TMP component, but I’m curious what the intended use of ITextPreprocessor is. Simply implementing the interface on an attached monobehaviour never sees it called.

I’ve thoroughly searched both Unity Answers and the TMP package documentation (and googled vigorously) and see nothing on how to actually utilize this interface.

Has anybody used it before? I would love to know how it’s meant to be used.

You assign your type implementing ITextPreprocessor to the textPreprocessor property of TMP_Text.

Example:

using TMPro;
using UnityEngine;

public class TextModifier1 : MonoBehaviour, ITextPreprocessor
{
    public string PreprocessText(string text)
    {
        return text.ToUpperInvariant();
    }

    private void Start()
    {
        GetComponent<TMP_Text>().textPreprocessor = this;
    }
}

Ah great, thank you. I actually found it shortly after posting (of course). It was not clear this is typically null and can be overridden. I was expecting a stack of preprocessors being an option, but I understand why that isn’t the case.

The hardest part of using this system is having to account for default TMP rich text happening after your preprocessing, which I’m still working through.