I have an empty GameObject
with Grid Layout Group
in it.
And several Sprite
children gameobjects with Layout Element
s in them.
I noticed that the Grid Layout Group
will "resize" them according to its setups.
But unfortunately, the Sprite
is unaffected by the "size", it is affected by the "scales".
And I can't find any options in neither Grid Layout Group
nor Layout Element
allowing me to set it as auto-rescaling instead of auto-resizing.
Nor could I find any options in Sprite Renderer
to let it "autofit" to its size.
I tried to add a script in the child objects rescaling them through code:
RectTransform rt = GetComponent<RectTransform>();
rt.localScale = rt.sizeDelta;
rt.sizeDelta = new Vector2(1, 1);
But for some reason, all the sizeDelta
s are all become (0,0) when it was assigned to the localScale
.
And the funny thing is, when I check it in the inspector after running, all the sizes in the Rect Transform
are what it supposed to be!
(So the result becomes: sizeDelta
unchanged, localScale
becomes (0,0,0).)
Could somebody please be so kind and teach me how to fix it?
Or maybe there are some options or other components I could add to the child objects to let them auto-rescale?
Thank you very much for your help!
CodePudding user response:
Layout groups are designed to be used with UI Elements so instead of sprite renderer you're expected to use Image that uses rect transform to control its size. For game objects using sprite renderer you're better off using grid component or creating custom component.
Now if you're making games like match three games you'll have to decide whether you want to use UI Elements for the tiles/orbs/fruits or sprites with grid or some sort of custom component. Both of these have their pros and cons.