Page 1 of 1

How To: Show Portrait Name Only For First Line

Posted: Thu Jul 16, 2020 9:33 am
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;
    }
}