How To: Show Portrait Name Only For First Line

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

How To: Show Portrait Name Only For First Line

Post by Tony Li »

Someone asked how to show actors' portrait names only the first time they speak in a conversation. If you don't mind doing a little scripting, you can do this automatically with a bit of code.

One way is to subclass StandardDialogueUI and override Open() and ShowSubtitle().

Override Open() to initialize a list of actors for which you've already shown their name:

Code: Select all

private HashSet<int> actorNamesShown = new HashSet<int>(); // List of actor IDs.
public override void Open()
{
    base.Open();
    actorNamesShown.Clear();
}
Override ShowSubtitle() to enable the subtitle panel's Portrait Name text element if the actor's name hasn't been shown yet, and disable it if it's been shown:

Code: Select all

public override ShowSubtitle(Subtitle subtitle)
{
    DialogueActor dialogueActor;
    var subtitlePanel = conversationUIElements.standardSubtitleControls.GetPanel(subtitle, out dialogueActor);
    subtitlePanel.portraitName.enabled = !actorNamesShown(subtitle.speakerInfo.id);
    actorNamesShown.Add(subtitle.speakerInfo.id);
}
If you're showing portrait names using the subtitle panel's Add Speaker Name checkbox, replace the portraitName.enabled line with a line to set the addSpeakerName bool instead

Alternatively, you can skip all that and add a small script the characters' GameObjects that uses OnConversationStart and OnConversationLine methods to prepend the actor's name to the subtitle text:

Code: Select all

public class PrependNameToFirstLine : MonoBehaviour
{
    public bool firstLine;
    
    // Note: This only work for the conversation's two primary participants.
    void OnConversationStart(Transform actor)
    {
        firstLine = true;
    }
    
    void OnConversationLine(Subtitle subtitle)
    {
        if (firstLine) subtitle.formattedText.text = subtitle.speakerInfo.Name + ": " + subtitle.formattedText.text;
        firstLine = false;
    }
}
Post Reply