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

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 23238
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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>";
            });
    }
}
Skermunkel
Posts: 7
Joined: Mon Jun 02, 2025 1:56 pm

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

Post 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 :)
User avatar
Tony Li
Posts: 23238
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Glad you found it useful! :-)
Post Reply