Page 1 of 1

[HOWTO] How To: Indent Text

Posted: Tue Dec 17, 2019 9:22 am
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.

Re: [HOWTO] How To: Indent Text

Posted: Tue Jun 22, 2021 2:35 am
by maloxplode
I feel dumb, but--
Where would we paste this code?

Re: [HOWTO] How To: Indent Text

Posted: Tue Jun 22, 2021 8:43 am
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>";
    }
}

Re: [HOWTO] How To: Indent Text

Posted: Tue Jun 22, 2021 2:43 pm
by maloxplode
Thank you so much!

Re: [HOWTO] How To: Indent Text

Posted: Tue Jun 22, 2021 3:25 pm
by Tony Li
Happy to help!

Re: [HOWTO] How To: Indent Text

Posted: Sat Apr 20, 2024 6:06 pm
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.

Re: [HOWTO] How To: Indent Text

Posted: Sun Apr 21, 2024 12:40 am
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.

Re: [HOWTO] How To: Indent Text

Posted: Wed Apr 24, 2024 4:12 pm
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!

Re: [HOWTO] How To: Indent Text

Posted: Wed Apr 24, 2024 4:44 pm
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.