I have been struggling with this exact problem multiple times, and I have never found a really good solution. In short, I have a GameObject which is an UI Image, this GameObject have multiple child objects, which are all of same type, when modifying the size (not scale!) of the parent, I would like the child object to be sized along, as if they were pixels on the parent image. How do I do that?
This behavior is easily done If the parent is just scaled, if i stretch it in width by increasing scale y, the children will be stretched as well. If i then increase scale x equal amount I am back to original aspect ratio of both parent and its children, everything is just bigger now.
My problem is that my parent width and height is based on very complicated UI aspect ratio fitters and custom scripts. So I cannot âjust use scaleâ.
Case example:
I have a yellow square 400x400, with a child, red square 100x100. red square is in pos x -100 y -100
If i increase yellow squares height to 500, the red squares height will be unchanged, while its desired height would be 120.
As you can see here, the red is still a square while the parent is a rectangle, i.e. the child does not scale like it was pixels in the parent image.
It should have looked like this:
Now, some of you might be quick and say âjust use anchorsâ. but that doesnât solve the problem. I can anchor the child to stretch the full width and height of parent, i then offset 50 pixel bottom and left, 250 pixels top and right. It now looks the same. If I increase the parents width the red square will also increase in width, but the offset is not increasing, as they are fixed values, breaking the composition.
example when increase parent width to 600
how it should look:
Again, I keep running in to this problem over and over again, in various setups. I hope that someone here has a dirty trick up the sleeve to fix this problem.





