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 Text Width

Discussion in 'UGUI & TextMesh Pro' started by WimLI, Mar 31, 2015.

  1. WimLI

    WimLI

    Joined:
    Mar 31, 2015
    Posts:
    9
    in my game i need show a talk dialog,I use the Text component to show letter,but in the letter there is some smile image(fixd width),these letter show one by one, How can I do this? i need get the text precise width to position the image,but I can't find a function in the Text component,how to do this?
     
  2. Yukichu

    Yukichu

    Joined:
    Apr 2, 2013
    Posts:
    420
    I don't know the 'correct' way to do it, as I could not find the text size accurately until the next frame when it's been positioned/fit.

    Set text
    Coroutine wait endOfFrame
    Either the panel the text is in, or the text itself (which is what I think you want) - rectTransform.rect.width
     
  3. WimLI

    WimLI

    Joined:
    Mar 31, 2015
    Posts:
    9
    i need the real text width,not the rectTransform.rect.width,infact the rectTransform.rect.width is large than the real text lenght
     
  4. louisgv

    louisgv

    Joined:
    Aug 7, 2013
    Posts:
    18
    A possible solution is to have a font material which translates several unneeded symbols into emoticons or smile images. Thus, you will be able to insert them into dialogs without having to worry about scaling the text to fit the emotions.
    Good luck,
     
  5. WimLI

    WimLI

    Joined:
    Mar 31, 2015
    Posts:
    9
    Hi louisgv,infact i only want to use system font, how to make a material to translates several unneeded symbols into emoticons ?
     
  6. WimLI

    WimLI

    Joined:
    Mar 31, 2015
    Posts:
    9
    I have try make a bmp font,it really work fine ,use material i can show letter and emoticons,but I find another problem,the character can't wrap , i'm sure i have set the "horizontal overflow" to "Wrap",and the Text rectTransform.rect.height is enough,but the text don't change line ,why?
     
  7. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Just in case you are open to alternative suggestions.



    Works with the new UI and allows you to add Sprites inline with the text. Sprites are added by using the <sprite=xx> tag where X is the sprite index in the Sprite Asset.

    Since the sprites are treat just like any other character, word warping and all other tags and things affecting characters also affects those sprites.

    As you can see in the GIF, the text is revealed one character at a time, respecting word wrapping.

    This is just one of the things that TextMesh Pro offers. Just let me know if you want more information.