Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scaling background with text: Detecting horizontal overflow?

Discussion in 'UGUI & TextMesh Pro' started by pea, Aug 27, 2014.

  1. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    Hi there! Love the new UI work, love it.

    I'm trying to create a text element that scales as more text gets entered into it. Something like this:

    [ Text box with stuff ]

    [ Text box with MOOOOORE stuff ]

    These are to be anchored to the right side of the screen, for use as a score indicator. And the background is a sliced sprite.

    Happy to clarify further if that'd help. Not having any luck getting the sliced background to "follow" the text width.
     
  2. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    Assuming the Text object is a child to the Image background, to the Image object add the components Layout - Horizontal Group and Layout - Content Size Fitter, and choose Horizontal fit - Preferred Width on the latter.
     
  3. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    That's great, thank you! Almost working, just have to tweak it somehow as it scales from the middle as opposed to from the right anchor outwards to the left.

    This is powerful stuff.
     
  4. pea

    pea

    Joined:
    Oct 29, 2013
    Posts:
    98
    Ah, got it. It was due to the scale on my image parent being different to the scale on the text child. Made it go reaaaal bad. Thanks again Melang!
     
  5. senthilkumar_unity542

    senthilkumar_unity542

    Joined:
    Dec 3, 2020
    Posts:
    1
    How can we do it in 2D World Space using SpriteRenderer and TextMeshPro?