Search Unity

How do I recognize the overflow of a Text component in the 4.6 UI API?

Discussion in 'Scripting' started by Tiki, Apr 1, 2015.

  1. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Pretty straight-forward question. I want to recognize when the text begins to overflow in a Text component of the canvas UI system through c# scripting. The idea is to write text to a page, recognize when it's reached the end of the page, and start writing on another page.

    Updates:It appears I can access the appropriate information I need through a TextGenerator:
    http://docs.unity3d.com/ScriptReference/TextGenerator.html

    Which the Text component appears to conveniently cache in this variable before rendering:
    http://docs.unity3d.com/ScriptReference/UI.Text-cachedTextGenerator.html

    So after some experimentation, this doesn't quite work. I was hoping that the cached TextGenerator would give more information regarding the vertical overflow truncation, instead it seems that the characterCount and characterCountVisible come out to the same value. How can I recognize the truncated segment of text?


    Bump: Really having an issue finding a working solution, any input welcomed.

    The idea is to write text to a page, recognize when it's reached the end of the page, and start writing on another page.

     
    Last edited: Apr 2, 2015
  2. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Bump: Anyone have any answers? I think I have a concept to go with - I want to grab the characters being drawn by the TextGenerator. Anyone know how to convert a UICharInfo List into a string? For instance - the characters array on TextGenerator.

    Bump: Who would of thought this would be such a difficult task to accomplish?
     
    Last edited: Apr 2, 2015
  3. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Bump: Please Help! Anyone!
     
  4. maxizrin

    maxizrin

    Joined:
    Apr 13, 2015
    Posts:
    20
    Maybe textComponent.cachedTextGenerator.characterCountVisible could help.
    But make sure a frame has passed after you assign the text, or use Canvas.ForceUpdateCanvases(); to have the real values from the cachedTextGenerator.
    And compare it to textComponent.text.length.
     
  5. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Having the same kind of issue with a chat addition, I'm going to have to say cachedTextGenerator won't be much help to you, and heres why. When I tested it, and checked it, wrong info, ran it again, it would correct. Its not that it doesn't have the right info, its that it doesn't seem to refresh as you need it, so its information can't be counted on...

    What I ended up doing is going with mono spaced fonts so I could keep up with the position based on string length (OH GOD NO!!!!) yea, but they have more variation then they used to have so I tried to compensate by having a selector that lets you choose which font it uses. =p

    best of luck, no its not at ALL what you were looking for, and for that I apologize, but its the best answer I found in the same position.
     
  6. maxizrin

    maxizrin

    Joined:
    Apr 13, 2015
    Posts:
    20
    I needed this for RTL languages, and I came up with another way.
    Disclaimer: I gave up on making the app in Unity, and used Android studio instead.
    You can check the width of every letter, and compare it to the width of the text component.
    You can get that from Font, in the text component.
    I'm afraid Unity is greatly lacking in text manipulation at the moment... especially for mobile.