Prefab does not scale in scale with screen size mode in UI
Asked Answered
M

2

0

In unity 2d UI I have switched the canvas scaler into scale with screen size mode.

For those components added into the canvas manually the scaler works well. But for those added by scripts, even from the same prefab, the scaler does not work.

The appearance of the script-added component depends on the resolution setup, and mostly looks differently to those added manually, like following:

[53031-屏幕快照-2015-08-29-上午13233.png|53031]

The first button is added manually and the rests are adde by scripts. I’ve set the button layout to be horizontally expanded so they should have fit the screen width but didn’t. Anyone have ideas this could be a bug or did I set anything wrong?

Maniac answered 14/9, 2023 at 21:19 Comment(1)

i might make a new project so ill tell you if i need help ok :D

Curia
S
0

I have same issue until i set

go.transform.SetParent( parent.transform , false );

And now works fine

Sterilant answered 27/11, 2023 at 23:20 Comment(5)

Thank you! Why this isn't covered in the Shop UI tutorial, I'll never know. If I had any reputation points to reward you, they'd be yours.

Steerage

This is still working as the answer in 2017. You are the real MVP!

Symposiarch

Thanks! Still the solution in 2018.3. For any other noobs out there, implementation looked like this for me: resultObj.transform.SetParent(resultsPanel.transform, false); Where resultObj was the object scaling incorrectly and resultsPanel was the parent is was being added to.

Sauer

Thank you so much! Been trying to figure out a weird bug for like one hour. Thanks again!

Outwit

It's 2023 now and this just helped me. Thanks.

Fortyniner
F
0

Thanks to @Sterilant’s answer I found out that

Instantiate(prefab, parentTransform); does the same as go.transform.SetParent(parent.transform, false);

and

Instantiate(prefab, parentTransform.position, parentTransform.rotation); is like go.transform.SetParent(parent.transform, true);.

So for me, I just had to switch to this instantiating method:
Instantiate(prefab, parentTransform);

Fortyniner answered 14/9, 2023 at 20:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.