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

Unity UI [HorizontalLayoutGroup] Image RectTransform size is bigger than the "actual" image

Discussion in 'UGUI & TextMesh Pro' started by ireth_86, Jul 30, 2018.

  1. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    Hello everybody.

    I'm trying to create a UI from script.
    I want to build a simple structure like this:

    -> Container (Horizontal Layout Group)
    --> image_0
    --> image_1
    --> ...

    My images are all different in terms of dimensions.
    When Unity puts the images into the Horizontal Layout Group gameobject, it often happens that the image itself has a lower size than its rect transform (as you can see in the picture, the "zero" image is preserving its aspect as desired, but the height is stretching until the end of the parent height):

    capt.JPG

    I need the rect transform to be the same width and height of the image, so that the container can fit on its children in a correct way, but I can't figure out where to access these information.

    Also, I don't understand how to make it work with the "Child Control Height" of Horizontal Layout Group. Apparently I can't deselect it, or my images' height value in rect transform will be 0. So, even if I had the "correct" image height, I couldn't set it on its rect transform.

    Thanks in advance for any suggestion.
     
  2. ireth_86

    ireth_86

    Joined:
    May 18, 2016
    Posts:
    23
    Ok I found out the solution. I'll leave it here just for the sake of completion.
    I've put all my loading code inside a coroutine, then when I create the container I uncheck "Child Control Height" and manually set the height, since I know its width (but only after WaitForEndOfFrame(), otherwise the sizeDelta will be (0,0)).