Hi,
is there any way to automatically scale sprites in child components relative to the parent component?
I’ve got an (empty) GameObject as parent, and a bunch of SpriteRenderer components as children. At runtime, I dynamically assign the (differently sized) sprites so they layer on top of each other (with the center as sorting point).
Unfortunately, any size restrictions imposed by my layouts only seem to affect the parent component, and not the children components. Am I missing something obvious in the editor or is what I’m trying to do not possible without scripting the behavior by hand…?
So maybe i am not understanding fully but you could probably rig a script to adjust the children’s X, Y and Z scale depending on the Parent’s X, Y and Z. Basically, have script on the parent that accesses the children object’s transform component. Then you have a simple formula in Update like: childXScale = parentXScale * 0.5f; or somethign like that.
I honestly have no idea if that is the best way but I think it could work.
Are you just using Z axis for sorting the draw order? If so switch to using SpriteRenderer larying instead and save yourself the headache.
The child objects when assigned to a parent should be adjusted when it is placed. For instance if you scale parent from 1 to 2 then add an object I believe you would see that child’s scale as 0.5 as it is resizing itself based on parent being 2.
Thank you, @Cornysam and @adehm ! I’m new to Unity, but I think I figured out what my initial mistake was: The parent object had a “Rect Transform” component, while the children had a “Transform” component - once I deleted the Rect Transform (replacing it with a “Transform”) on the parent, the children automatically properly scaled with it. I didn’t know/notice that those are functionally different.