Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

TextMesh Pro Weird behavior with TMP "reveal" effect in combination with middle alignment

Discussion in 'UGUI & TextMesh Pro' started by csumsky3, Feb 8, 2019.

  1. csumsky3

    csumsky3

    Joined:
    Jan 29, 2014
    Posts:
    13
    Hello!

    Apologies if this has been addressed elsewhere, I've looked for a long time and can't seem to find a solution!

    I have a simple "reveal"/"teletype" effect going on for a TextMeshProUGUI object, where the alignment of the object in the inspector is set to "Middle". The revealing is working great, except for lines greater than 1, in which case the object doesn't seem to know ahead of time that there are going to be multiple lines, so it'll start printing as 1 centered line, then when the line breaks the whole object readjusts to fit 2 lines centered. If that's hard to parse, I have a little video :)

    https://www.dropbox.com/s/80s5w3jo5w4jcpb/dialogue printing.mp4?dl=0

    The TMP object settings are so:

    tmp settings.JPG

    And my printer script looks like this:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6. using geode;
    7.  
    8. public class BTextPrinter : MonoBehaviour
    9. {
    10.     const float waitPerCharacter = 0.025f;
    11.  
    12.     [SerializeField] TextMeshProUGUI textToPrint = null;
    13.  
    14.     bool canSkip;
    15.     bool skipped;
    16.  
    17.     void OnEnable()
    18.     {
    19.         BUIModule.OnSubmitPress += onSkipPressed;
    20.         BUIModule.OnCancelPress += onSkipPressed;
    21.     }
    22.     void OnDisable()
    23.     {
    24.         BUIModule.OnSubmitPress -= onSkipPressed;
    25.         BUIModule.OnCancelPress -= onSkipPressed;
    26.     }
    27.     public void ClearText()
    28.     {
    29.         textToPrint.text = "";
    30.         textToPrint.ForceMeshUpdate();
    31.     }
    32.     public Coroutine SetAndPrintText(string _text)
    33.     {
    34.         canSkip = false;
    35.         skipped = false;
    36.         textToPrint.text = _text;
    37.         textToPrint.ForceMeshUpdate(false);
    38.         return StartCoroutine(print());
    39.     }
    40.     IEnumerator print()
    41.     {
    42.         int totalVisibleCharacters = textToPrint.textInfo.characterCount;
    43.         int counter = 0;
    44.         while (true) {
    45.             if (skipped) {
    46.                 skipped = false;
    47.                 textToPrint.maxVisibleCharacters = totalVisibleCharacters;
    48.                 break;
    49.             }
    50.             int visibleCount = counter + 1;
    51.             textToPrint.maxVisibleCharacters = visibleCount;
    52.             if(visibleCount >= totalVisibleCharacters) {
    53.                 break;
    54.             }
    55.             counter++;
    56.             yield return new GTime.WaitForSeconds(waitPerCharacter);
    57.             //Wait one character to set this true to provide a little buffer for the skip button press
    58.             canSkip = true;
    59.         }
    60.         canSkip = false;
    61.         //Wait one frame so skip button presses don't interfere with progressing dialogue
    62.         yield return null;
    63.     }
    64.  
    65.     #region Button Listeners
    66.     void onSkipPressed()
    67.     {
    68.         if(canSkip) {
    69.             skipped = true;
    70.         }
    71.     }
    72.     #endregion
    73. }
    74.  
    This script is largely based off of this older video I found
    .

    Any suggestions for why it wouldn't know the line count and positioning of the text ahead of time? I sorta was under the impression that was the benefit of printing this way. Other good things work, like how a word that will break onto the new line knows ahead of time that it would do that, and starts printing on the new line. Just curious why the line alignment bit wouldn't work.

    Some other research suggests that I might need to go a different route if I want it to behave this way, perhaps by modifying the vertex attributes (
    , https://forum.unity.com/threads/teletype-interfering-with-vertex-jitter.611245/#post-4092169). This quote from Stephan especially makes me think this is the case: "using .maxVisibleCharacters ... ends up forcing a regeneration of the text object which in turn resets the geometry."

    Sorry for the long post! Any help is greatly appreciated :)

    Chris
     

    Attached Files: