I'm having really persistent problems with layout groups & Scroll Rects......

I’m having a lot of issues with layout groups and scroll rects. Can anyone help?

Layout Groups

When I first add button items to one using this code, they look fine. see top image.

Instantiate(prefab, target).transform.SetParent(target, false);

8911987--1220425--upload_2023-3-29_14-50-43.png

However, if I try to move them from one group to another (like in an object sorting game) this often happens:

8911987--1220422--upload_2023-3-29_14-49-38.png

The only way to fix this is to add another icon, or change the screen size by double-clicking the game window tab. The code that moves the icon is this:

rect.transform.SetParent(target, true);
rect.DOJumpAnchorPos(Vector2.zero, 100f, 1, Options.uiSpeed / 1.3f).SetEase(Ease.OutQuad);

And the layout group is just this:

8911987--1220428--upload_2023-3-29_14-51-38.png

Scroll Rects

These seem to make the x or y position of the container go absolutely wild. The position will rapidly increase forever, even outside playmode. I’ve tried them many times, but something seems broken.

I think you don’t want those ForceChildExpand boxes checked.

These Layout controller thingies are a little bit “thenthitive” to fiddle with. It seems every time I go to use one I have to relearn it. I recommend making a blank scene and just go nuts making these things again and again, inserting items into them, trying the different options, etc.

The other thing is to make sure your anchoring is correct and to know that the layout thingy WILL control your children objects in various ways: you’ll see various parts of the RectTransforms of children grayed out.

1 Like

Ah, so it’s not just me! Thank you, but unticking them didn’t help.

Any idea why the scroll rect does that? Or how I can stop it?

Generally when any of the UI stuff malfunctions spectacularly, I sniff around to see if I can figure out where it went wrong, then I just delete it and recreate it.

One thing that can really bork things is if you ever set a scale (or any part of a scale) to zero. If you animate something to vanish by scale zero, or if you hide something by scale zero, you can explode all kinds of stuff, and if you save the scene in that state, you may be very sad.

The proper way to set scales:

https://discussions.unity.com/t/851227/2

NEVER set scale (or any part of scale) to zero or negative.

NEVER use Vector2 because that makes the third term (Z) zero.

ALWAYS use Vector3, and make sure that the .z is either 1.0f, or else your non-zero scale.

Similarly never set scale to Vector3.zero, especially in a UI, because this will cause layout divide-by-zeros and damage all your hierarchy.

Oh no. I use zero all the time. It may be why… So I can’t hide things by shrinking them at all? Or is it okay if I do new Vector3(0.0001, 0.0001, 0.0001) or similar? Or does z always have to be 1?

How do you feel about using LayoutRebuilder.MarkLayoutForRebuild(layoutGroupTransform.GetComponent<RectTransform>()); to fix the weird issues? It seems to work tbh.

Hi the Canvas scaler and the horizontal layout group should work together to keep all UI elements in line with minimal coding.

so make sure you have tuned them correctly.
I like to make a canvas, make handles and the like, things I could parent to, and make it so I can switch aspects and resolutions and it gives me a close or exact match for a usable UI. It kicks you if you forget about it. And can feel buggy to work with. But the layout groups, layout elements and canvas scaler component along with correct parenting should handle all of the issues nicely in the end.

1 Like

The second link was helpful generally, thank you, but I’m still not sure what the best thing to do regarding size of UI elements is. Is there a minimum acceptable size? Or should I avoid minimising them entirely?