Page 1 of 1

[HOWTO] How To: Add Tags to Keywords in Subtitle Text

Posted: Sat May 24, 2025 10:22 am
by Tony Li
If you want to wrap special formatting codes around key words or phrases in subtitle text at runtime before displaying the text, add a script with an OnConversationLine(Subtitle) method to your Dialogue Manager GameObject or hook into the C# event DialogueManager.instance.conversationLinePrepared. Here's a simplified example:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
public class HighlightKeywords: MonoBehaviour
{
    void OnConversationLine(Subtitle subtitle)
    {
        // Make the word "quest" wiggle:
        subtitle.formattedText.text = subtitle.formattedText.text.Replace("quest", "<wiggle>quest</wiggle>");
    }
}
The example above is simplified to make it easier to understand. In practice, you'll probably want to use Regex to match words. Otherwise, for example, the code above will change "request" to "re<wiggle>quest</wiggle>", and it doesn't handle uppercase letters. Instead use something like the code below to only wrap codes around "quest" if it's an individual word:

Code: Select all

using System.Text.RegularExpressions;
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class HighlightKeywords: MonoBehaviour
{
    void OnConversationLine(Subtitle subtitle)
    {
        subtitle.formattedText.text = Regex.Replace(
            subtitle.formattedText.text,
            @"\bquest\b",
            match =>
            {
                return $"<wiggle>{match.Value}</wiggle>";
            });
    }
}

Re: [HOWTO] How To: Add Tags to Keywords in Subtitle Text

Posted: Tue Jun 03, 2025 1:48 pm
by Skermunkel
Thank you again so much for this one Tony, you posted this before I joined the forum and I forgot to thank you :)

Re: [HOWTO] How To: Add Tags to Keywords in Subtitle Text

Posted: Tue Jun 03, 2025 2:56 pm
by Tony Li
Glad you found it useful! :-)