How to change actor font through ActorSubtitleColor

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
FlakeyJakey
Posts: 5
Joined: Sat Oct 23, 2021 7:04 pm

How to change actor font through ActorSubtitleColor

Post by FlakeyJakey »

On this thread a while back, you mention that you can assign a font by duplicating and changing the ActorSubtitleColor script. I looked in the script and was wondering how exactly I could do that.

Thank you!
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to change actor font through ActorSubtitleColor

Post by Tony Li »

Hi,

Here are three ways to change fonts:

1. Use different subtitle panels for different actors. Assign a different font to each subtitle panel.

2. Or, if you're using TextMesh Pro, make a new script like ActorSubtitleColor that wraps the text in <font> tags (see note) similarly to how ActorSubtitleColor.ProcessText wraps the text in color tags. This is the best method to use if you're accumulating text like the Runic or WRPG dialogue UI prefabs do. The ProcessText method might look something like:

Code: Select all

public string fontAssetName; // Set in inspector.
...
private string ProcessText(Subtitle subtitle)
{
    return $"<font='{fontAssetName}'>{subtitle.formattedText.text}</font>";
}
3. Or make a new script like ActorSubtitleColor that changes the subtitle panel's font in ProcessText. The ProcessText method might look something like:

Code: Select all

public Font font;
...
public string ProcessText(Subtitle subtitle)
{
    // Get the subtitle panel:
    DialogueActor dialogueActor;
    var panel = DialogueManager.standardDialogueUI.conversationUIElements.standardSubtitleControls.GetPanel(subtitle, out dialogueActor);
    // And set its subtitle text font:
    panel.subtitleText.uiText.font = font; // Assumes UI Text. Set textMeshProUGUI property instead if using TMPro.
}
FlakeyJakey
Posts: 5
Joined: Sat Oct 23, 2021 7:04 pm

Re: How to change actor font through ActorSubtitleColor

Post by FlakeyJakey »

When I use the code below, I get an error with the text where it only puts in the first part of the font indicator (image attached). There's probably something extremely simple that i'm missing but I cant quite figure it out.


using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

namespace PixelCrushers.DialogueSystem
{

public class ActorFont : MonoBehaviour
{
public string fontAssetName;

public void OnConversationLine(Subtitle subtitle)
{
CheckSubtitle(subtitle);
}

private void CheckSubtitle(Subtitle subtitle)
{
if (subtitle != null && subtitle.speakerInfo != null && subtitle.speakerInfo.transform == this.transform)
{
subtitle.formattedText.text = ProcessText(subtitle.formattedText.text);
}
}

private string ProcessText(string text)
{
return $"<font='{fontAssetName}'>{text}</font>";
}

}
}

Thank you for your help so far!
Attachments
an example of what my dialog system looks like when i initiate a conversation
an example of what my dialog system looks like when i initiate a conversation
example.PNG (14.48 KiB) Viewed 448 times
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to change actor font through ActorSubtitleColor

Post by Tony Li »

Hi,

Try double quotes:

Code: Select all

return $"<font=\"{fontAssetName}\">{subtitle.formattedText.text}</font>";
(Maybe TMPro doesn't accept single quotes?)

Also, make sure your font is in a Resources folder as described in the (see note) link above. This link might have a more helpful screenshot: https://forum.unity.com/threads/font-ma ... st-4024261
FlakeyJakey
Posts: 5
Joined: Sat Oct 23, 2021 7:04 pm

Re: How to change actor font through ActorSubtitleColor

Post by FlakeyJakey »

Thank you so much! You were right, for some reason TMP doesn't support single quotes.
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to change actor font through ActorSubtitleColor

Post by Tony Li »

Glad to help!
Post Reply