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

TextMesh Pro TextMesh Pro with ScrollRect

Discussion in 'UGUI & TextMesh Pro' started by kabumere, Nov 4, 2017.

  1. kabumere

    kabumere

    Joined:
    Oct 2, 2016
    Posts:
    31
    I initially created a ScrollView (scroll rect) using Unity UI, and replaced the Content child object inside the ViewPort with my TextMesh Pro UGUI Text object and got it to work. Afterwards I looked at the TMP examples just to see how it was accomplished by the creator out of curiosity, and saw a 2D Rect Mask was used directly on the object using the scroll rect script (I believe in lieu of the Viewport object, which is used as a mask).

    1) Is this a cleaner/more efficient way to use Scroll Rects with TMP? Are there any drawbacks to using the Viewport object as my mask (doesn't use 2D Rect Mask)? This is for a 2D game by the way.

    2) Is there a way to make the Rect Transform of the TMP UGUI Text object auto scale to fit all text inside the object? I noticed with my original setup using Unity's Scroll View, if I manually made the Rect Transform larger than the text that was being displayed, then the user could still scroll and just see nothingness. I want to prevent that by having the box scale with the text - if there's only a little bit of text, then the box won't go past the viewport and scrolling will be disabled. However if there's a lot of text, it will go past the viewport and scrolling will be allowed. I looked at the examples of word wrapping but none seemed to do it.
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    816
    1) I mostly use a simple Rect Mask 2D in the viewport object (parent of Content). This way I can easily control how much is visible from the scroll rect and it is not using any stencil magic and therefore is more performant than a mask component.
    2) Use a content size fitter with preferred size for vertical fit:
    upload_2017-11-6_9-1-13.png
     
    AdamFoster and LiquidEggProduct like this.
  3. kabumere

    kabumere

    Joined:
    Oct 2, 2016
    Posts:
    31
    Is the difference in performance between the 2 methods significant at all, mobile or otherwise?
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    The 2D RectMask does offer better performance over the Stencil mask.
     
    elenzil likes this.
  5. kabumere

    kabumere

    Joined:
    Oct 2, 2016
    Posts:
    31
    So I replaced the Mask on my Viewport with the RectMask2d and it works. However I have an issue with the content size fitter.

    So using preferred size for vertical height does indeed get the rect transform and the yellow margin borders to exactly fit the text in the TMP UGUI object. However it doesn't keep its position relative to the viewport, it always resets itself to 0X, 0Y. I want to keep the top border of the TMP object lined up with the Viewport while using a content size fitter.

    Changing the anchor presets from stretch, to center middle, center top, etc etc doesn't change the issue of it resetting its position every play.

    EDIT: Well, learned about pivots. For anyone else experiencing this, when you click the anchor preset icon, holding shift allows you to set a new pivot for the object. I set the pivot to the upper left corner, so now when it's reset to (0,0) it treats the origin as upper left instead of middle, and stays put. Still researching whether the constant position resets to the origin should even be happening or not, though.
     
    Last edited: Nov 9, 2017
  6. Zymes

    Zymes

    Joined:
    Feb 8, 2017
    Posts:
    106
    How do you get this to work with dynamic text that you add via script?
     
  7. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    816
    The content size fitter can also handle changes. So basically the same way.
     
    josiperez likes this.
  8. kabumere

    kabumere

    Joined:
    Oct 2, 2016
    Posts:
    31
    Hey Zymes, I can confirm Johannski's answer. That was actually my reason for trying to find this functionality - I have text added dynamically and I needed the rect transform to always fit the text exactly even when changed.
     
  9. superflyninja

    superflyninja

    Joined:
    Mar 8, 2013
    Posts:
    24
    Would one of you guys mind uploading a screenshot of the setup please? Im having a little trouble getting TMP working with scrollview.
    Cheers!
     
  10. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    659
    @Johannski Thank you! This had been bothering me with not only Text, but other stuff!

    Here's a screenshot. I "Stretch/Stretch" the content holder to fit the parent. Then "Bottom Stretch/Stretch" the content holder to Fix to bottom.

    No layout stuff are attached to TMP text -just a plain old TMP text.
     

    Attached Files:

    Last edited: Feb 1, 2018
    billy-goodluck-games and xyzDave like this.
  11. CrispyCritter

    CrispyCritter

    Joined:
    May 11, 2018
    Posts:
    19

    Thank you this soved my problem
     
    FlightOfOne likes this.
  12. billy-goodluck-games

    billy-goodluck-games

    Joined:
    Jun 6, 2022
    Posts:
    1
    Thanks, I had the Content Fitter but not eh Horizontal Layout group. Much appreciated