Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Get Truncated String

Discussion in 'UGUI & TextMesh Pro' started by khanstruct, May 8, 2015.

  1. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    I've looked everywhere, but can't find a solution even remotely close to what I'm looking for.

    Building my dialogue system, I want to display text and, if it's too long for the dialogue box, truncate it and store the truncated text in a variable (to be displayed next).

    So! Is there a way to get the truncated string?
    Or! Is there at least a way to find out if the string has been truncated?
    Or! At the very, very least, is there a way to get the pixel size of a string? (preferably with the option of multi-line sizes too).

    I miss CalcHeight :(

    Thanks!
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  3. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,870
    After a bit more digging, I found this:
    Code (CSharp):
    1. cachedTextGenerator.characterCountVisible
    which returns the number of characters that were actually displayed. You could then take the full string minus the number displayed and get a substring of the remaining, undisplayed text.

    My only issue now is that this only returns the displayed number AFTER its been displayed, which prevents me from displaying any kind of prompt for the remaining text along with it. Back to tinkering!
     
  4. tlutz

    tlutz

    Joined:
    Oct 14, 2013
    Posts:
    8
    I know this is an old post, but for those that run into this, here's how I got around it:

    Code (CSharp):
    1. text01.text  = someString;
    2.  
    3. //force canvas update so we can get correct result from cachedTextGenerator
    4. Canvas.ForceUpdateCanvases ();
    5. int  truncateIndex  = text01.cachedTextGenerator.characterCountVisible;
    6.  
    7. text01Continued.text = someString.Substring (truncateIndex);
     
    codxr, Voltion and eses like this.
  5. Voltion

    Voltion

    Joined:
    Jul 15, 2017
    Posts:
    1
    How long i looking for this solution, thank you so much