- Home /
instantiated button scaling issue
I have a button that is being instantiated within a scroll view . it has Layout Element attached to this Prefab button so I have to specify a width and height for it but the button is only being set correctly on 1 resoultion because if the resolution goes up or down the button are getting shifted up and down right and left . in short it's a mess the button doesn't scale with the screen resolution. Kindly if someone could help me I appreciate it becauase i can't find a way to fix this.
Answer by Ruri-Dev · May 09, 2017 at 08:22 PM
When using Instantiate for UIElements be sure to use the method-version: Instantiate(Object original, Transform parent, bool worldPositionStays) and setting the last bool to false to avoid issues. About the resolution scaling, use a CanvasScaler, you can configure it to scale UI depending on the screen size and using a reference resolution, the match mode determines how width and height are taken into account when calculating a scale.
If you are using LayoutElement then be sure to include a VerticalLayout or HorizontalLayout as well as a ContentSizeFitter into the ScrollView > View > Content, this will help manage the layout and sizes of the buttons.
There's plenty of information about putting buttons or other UIs into a ScrollView, I recommend checking the always-useful Unity Docs about the subject, here is a detailed explanation of the layout control: https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html
There's also this cool Unity video-tutorial for example. It may appear to be a lot of reading/watching but in the end one should be more concerned about really understanding how things work than just hot-fixing.
Thank your for your great reply I'm acctually depending on the video you put the link on and I'm having this issue when reducing the resolution
I've set the parameter to false and the button now aligned perfectly but when I press the button they get smaller on every click which is very weird.