Search Unity

Have child Text object scale parent Image?

Discussion in 'UGUI & TextMesh Pro' started by netravelr, Aug 21, 2014.

  1. netravelr

    netravelr

    Joined:
    Jan 1, 2013
    Posts:
    19
    So I have a text box in my game that I want to modify the size of the image its a child of based on the text that it contains (the background is already sliced and works great)

    Right now I have it working so that if I change/scale/whatever the parent the text will move correctly, but I want to do it the other way around, that is have the text change the the horizontal size of its parent.

    It would work if I made the background have the text as its parent but due to the way its rendered the image would be drawn in front of the text. I could write a script to update it, but that kind of defeats the purpose of having the new GUI system.

    Any help would be appreciated!
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Hi, if you look in the example project there is a scene called Layout Group. If you look at the Okay and Cancel buttons on the bottom they use layout components to achieve what you describe. You should be able to do the same thing in your scene.
     
  3. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    In addition to what Tim said - if you don't otherwise use layout groups in your scene, you'll need to use a ContentSizeFitter on the background as well.

    So on your background GameObject you can add a HorizontalLayoutGroup and a ContentSizeFitter set to preferred width and height. If you want padding around the text, you can control it using the padding setting on the HorizontalLayoutGroup.
     
    Kayumus likes this.
  4. netravelr

    netravelr

    Joined:
    Jan 1, 2013
    Posts:
    19
    The layout groups and the content size fitter works perfectly! However, I also have a child to the background image that I want to place above the textbox but move vertically when the size increases, this worked before due to the anchors I set, but I do not want it to be part of the layout group (displayed to the right of my text). My thought was to create a vertical group and then place the name on top followed by the text, but due to the Content Filler I get a warning saying I shouldn't do that. Any thoughts? Thanks!

    P.S.: Example 1 is what I want to have happen, Example 2 is what happens if I put it into the Box (how it moved correctly before)
     

    Attached Files:

    Last edited: Aug 22, 2014
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    If you want a child of a layout group to not be controlled by that layout group, you can add a Layout Element component to it (the child) and check the option to ignore layout.
     
    Kayumus likes this.
  6. netravelr

    netravelr

    Joined:
    Jan 1, 2013
    Posts:
    19
    So far so good! I got that figured out by now.

    Now, I want to be able to able to scale horizontally within a vertical group, but not modify the health. As it stands now if I have 4 options in my buttons list, it looks perfect:
    Screenshot 2014-10-23 21.34.55.png
    However, if/when I remove one element from it:
    Screenshot 2014-10-23 21.35.02.png
    I could do this easily in the old GUI system using the GUILayout and adding space to the bottom and top, but the UI system complained if I added a Size Content Filler. Any ideas? Thanks!
     
  7. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I'm not sure I understand exactly what the problem is. Each child seems to be getting taller, is that the problem? If so you can disable Force Child Expand Height on the Vertical Layout Group.
     
    Kayumus likes this.
  8. netravelr

    netravelr

    Joined:
    Jan 1, 2013
    Posts:
    19
    Got it! It wasn't available in beta 17, Upgraded to 20 and it's there now. Works great! Thanks!
     
  9. Kayumus

    Kayumus

    Joined:
    Jul 21, 2020
    Posts:
    15
    Hello

    I am having problem related to this topic. Can you help me please?
    I added a HorizontalLayoutGroup and a ContentSizeFitter to parent Image Object.
    Everything works well. However, If the text is too big, the parent Image object keeps scaling out of the screen.
    How to limit scaling of the parent Image Object?

    upload_2022-4-2_7-57-4.png

    What I want is like that

    upload_2022-4-2_7-54-7.png
     
  10. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    If it's not solved by the same solution then it's not the same problem, and you're better off starting a new thread than resurrecting an ancient one. Also, I don't work for Unity anymore. I hope you'll find the help you need.
     
    Kayumus likes this.