[HOWTO] How To: Indent Text

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

[HOWTO] How To: Indent Text

Post by Tony Li »

This is a quick one. Sometimes you want to indent text like:

Code: Select all

KIM KITSURAGI - "No need to worry," the 
    lieutenant steps in, "we''re not saying you did."
If you're using TextMesh Pro (see TextMesh Pro Support), you can use the <indent> tag, such as:

Code: Select all

void OnConversationLine(Subtitle subtitle)
{
    subtitle.formattedText.text = subtitle.speakerInfo.Name + " - <indent=10%>" + subtitle.formattedText.text + "</indent>";
}
The code above will automatically prepend the speaker's name and indent lines 2+ if the text wraps.

Note: If your indented text overlaps the name, you'll need to fix a bug in TextMesh Pro:

1. Make the TextMesh Pro package editable. See How can I modify built-in packages?

2. Edit TMP_Text.cs. Around line 7000-8500, locate this line:

Code: Select all

m_xAdvance = tag_Indent;
Change it to:

Code: Select all

if (m_xAdvance < tag_Indent)
{
    m_xAdvance = tag_Indent;
}
Meaning: only if the cursor is not yet further than the requested indentation, move it to the desired position; otherwise ignore it for the current line.
maloxplode
Posts: 2
Joined: Tue Jun 22, 2021 2:31 am

Re: [HOWTO] How To: Indent Text

Post by maloxplode »

I feel dumb, but--
Where would we paste this code?
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Indent Text

Post by Tony Li »

Not a dumb question at all. I didn't include that info in the post above.

OnConversationLine is one of the special methods that the Dialogue System will call in any script that's on the Dialogue Manager GameObject's hierarchy or the current conversation's primary actor and conversant.

Create a new script, and add it to the Dialogue Manager GameObject -- or, better yet, create a new prefab variant for your customized Dialogue Manager, and add it to that prefab so you can drop the same prefab in all of your scenes. The script should look something like:

AddNameAndIndent.cs

Code: Select all

using UnityEngine;
using TMPro;
using PixelCrushers.DialogueSystem;

public class AddNameAndIndent : MonoBehaviour
{
    void OnConversationLine(Subtitle subtitle)
    {
        subtitle.formattedText.text = subtitle.speakerInfo.Name + " - <indent=10%>" + subtitle.formattedText.text + "</indent>";
    }
}
maloxplode
Posts: 2
Joined: Tue Jun 22, 2021 2:31 am

Re: [HOWTO] How To: Indent Text

Post by maloxplode »

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

Re: [HOWTO] How To: Indent Text

Post by Tony Li »

Happy to help!
Eldritch_Horror
Posts: 28
Joined: Sun May 28, 2023 8:30 pm

Re: [HOWTO] How To: Indent Text

Post by Eldritch_Horror »

This has worked, great Tony. The only issue Im having is that I'm getting the names twice, once coloured, and once not. Do you have any suggestions?
Thank you.
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Indent Text

Post by Tony Li »

Since the script prepends the name, make sure these other features don't also prepend the name:
  • In dialogue UI, subtitle panel's Standard UI Subtitle Panel component > Add Speaker Name.
  • On actor, Dialogue Actor component's Set Subtitle Color -> Apply Color To Prepended Name.
  • Any custom scripts that might be prepending it, typically in an OnConversationLine(Subtitle) method.
Eldritch_Horror
Posts: 28
Joined: Sun May 28, 2023 8:30 pm

Re: [HOWTO] How To: Indent Text

Post by Eldritch_Horror »

Deactivating the colour prepend on the characters solved the issue. All dialogue is now the set colour. Do you know how I can set the spoken text back to white, and also, how can you prevent the display of the speakers name on a second line of dialogue, if one character speaks two sectioone after the other?
Thanks Tony!
Attachments
Screenshot 2024-04-24 150825.png
Screenshot 2024-04-24 150825.png (85.3 KiB) Viewed 793 times
Screenshot 2024-04-24 150617.png
Screenshot 2024-04-24 150617.png (102.16 KiB) Viewed 793 times
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Indent Text

Post by Tony Li »

Hi,

You can use a script like in How To: Color Actor Name and Indent Text. That thread include an example from April 9, 2024.
Post Reply