Search Unity

Scale background image based on text length? (Unity GUI)

Discussion in 'UGUI & TextMesh Pro' started by jvhgamer, Oct 4, 2017.

  1. jvhgamer

    jvhgamer

    Joined:
    Jun 27, 2017
    Posts:
    36
    EDIT:: https://ibb.co/kJKe8w

    More complete cohesive gif of what I am trying to accomplish.

    So I have a GUI text box in my game that I want to modify the size of the image behind it based on the length of the text.

    That is, I want to have the text change the horizontal size of the background dynamically.

    Visual depicting problem:
    https://ibb.co/geUhbG

    I've tried tinkering with HorizontalLayoutGroup and ContentSizeFitter, parenting the background to the text, etc etc. I was able to get close to the behavior I wanted in that as my text length increased, the image behind it did as well, but it scaled incorrectly.

    Visual depicting the closest I've got to the desired solution:
    https://ibb.co/n8zLVb

    Any help would be appreciated!

    I should note I have as well tried playing with the anchor and pivot point presets, but I am not adept with these tools.

    I found this gentleman's post here, https://forum.unity.com/threads/have-child-text-object-scale-parent-image.263465/, where he has a very similar question. Been trying to implement the process they took.

    As well, I've been reading through Unity's documentation on "Making UI elements fit the size of their content". Obviously I am failing to do just that.
     
    Last edited: Oct 4, 2017