Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question TMPRO: How to make textbox same width as the actual text?

Discussion in 'UGUI & TextMesh Pro' started by pix123123123, Aug 7, 2022.

  1. pix123123123

    pix123123123

    Joined:
    Sep 29, 2021
    Posts:
    3
    Hi all,
    I just have a quick question: So right now, when a player is given some choice options, I want it to make it so that the button's red highlight (which is just a UI button with a highlight color) is the same WIDTH as the text itself. So ideally, the button would dynamically change its width to the width of the text. So the red highlight would end right at where the text ends at the word "choices," rather than extending all the way towards the end of the choice panel
    upload_2022-8-6_19-58-37.png

    I made a quick mock (in paint lol) of what I want to achieve if I didn't explain it well: The button's highlight ends where the text ends. Is it possible to dynamically change the width like that?
    upload_2022-8-6_19-57-53.png
    I currently have a vertical layout group on my choice panel, and the buttons are dynamically generated from a prefab, so if there are four choices, the choice panel will have 4 buttons vertically laid out properly, or if there are two, it will look like the above screenshot.
    upload_2022-8-6_20-2-30.png
     

    Attached Files:

  2. pix123123123

    pix123123123

    Joined:
    Sep 29, 2021
    Posts:
    3
    Fixed the problem: created onselect event trigger on button, disabled the button onselect options, created a quick script for highlighing the text with <mark/>, and on deselect, deleting the </mark> in the text.