Page 1 of 1

[HOWTO] How To: Limit Text Length With Accumulate Text Option

Posted: Fri May 06, 2022 2:09 pm
by Tony Li
Everything that Unity displays, including text, is rendered as a collection of polygons defined by vertices. Each line of text adds to the vertex count. A single Text component can have up to 65000 vertices.

When ticking Accumulate Text on a subtitle panel, it's possible that the amount of text can exceed 65000 vertices.

If you switch to TextMesh Pro (see TextMesh Pro Support), you should be able to maintain longer text.

Alternatively, or in addition, you can add a small script to the dialogue UI that lops off old text as you add new text beyond a certain limit. For example, you can add this script to the Dialogue Manager:

LimitSubtitleTextLength.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class LimitSubtitleTextLength : MonoBehaviour
{
    public int maxLines = 50;

    private int numLines;

    void OnConversationStart(Transform actor)
    {
        // When we start a conversation, reset the line count.
        numLines = 0;
    }

    void OnConversationLine(Subtitle subtitle)
    {
        if (string.IsNullOrEmpty(subtitle.formattedText.text)) return;
        if (numLines < maxLines)
        {
            numLines++;
        }
        else
        {
            // If we're at the max number of lines, remove the first line from the accumulated text:
            var subtitlePanel = DialogueManager.standardDialogueUI.conversationUIElements.defaultNPCSubtitlePanel;
            subtitlePanel.accumulatedText = subtitlePanel.accumulatedText.Substring(subtitlePanel.accumulatedText.IndexOf("\n") + 1);
        }
    }
}

Alternatively, you can use the SMSDialogueUI subclass of the StandardDialogueUI class. SMSDialogueUI adds separate text instances for each line of dialogue. However, when the list of text instances grows large, the subtitle panel's Scroll Rect performance will suffer. To resolve this, you can use the Enhanced Scroller (paid asset) technique included in the Textline example project on the Dialogue System Extras page.