[HOWTO] How To: Add Tags to Keywords in Subtitle Text
Posted: Sat May 24, 2025 10:22 am
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:
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 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>");
}
}
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>";
});
}
}