When using a scaled canvas in Unity, it will adapt pixels to fit the screen, so we get effects like this:
The above is a Horizontal layout group with 2px padding, but this only works at the reference resolution of 1920x1080:
Other resolutions it can appear like the first image above, where some of the bars are displayed differently even though they are the exact same size.
Is there any way I can solve this? Using a Constant pixel size canvas is not an option. Using pixel perfect canvas does not solve the issue.
When using a scaled canvas in Unity, it will adapt pixels to fit the screen, so we get effects like this:
The above is a Horizontal layout group with 2px padding, but this only works at the reference resolution of 1920x1080:
Other resolutions it can appear like the first image above, where some of the bars are displayed differently even though they are the exact same size.
Is there any way I can solve this? Using a Constant pixel size canvas is not an option. Using pixel perfect canvas does not solve the issue.
Share Improve this question edited Jan 26 at 18:29 Majs asked Jan 22 at 11:33 MajsMajs 663 gold badges9 silver badges23 bronze badges 7- Hi the problem is not the padding, the elements in the layout have differents sizes. – Mario Commented Jan 23 at 4:48
- @Mario Yes but the problem is the same, its caused by what is described in the question. – Majs Commented Jan 23 at 15:00
- Upload both images to understand better the difference between both – Mario Commented Jan 24 at 17:34
- Have you tried forcing the width / height in the vitality bar layout group to be set to 'Child Force Expand' and 'Control Child Size'? Then if you want to correct the height size, you can create a new parent for the bar horizontal layout group, set it to the size you prefer, then set the bar layout group to expand itself within this new parent. If you want an example of this setup to test I can toss something together – TEEBQNE Commented Feb 8 at 21:40
- @TEEBQNE is not a problem that is only for layout groups. If I want a single line in my UI, for example to separate one section from another, I cannot make it 1px because it will disappear on some aspect ratios. – Majs Commented Feb 10 at 11:28
2 Answers
Reset to default 3 +200Using a scaled canvas, causes Unity to adapt pixels to fit the screen. This can cause imperfections as is seen in your case.
Here are some options:
- Enable Pixel Perfect or Canvas Pixel Perfect
E.g. See https://docs.unity3d/Packages/[email protected]/manual/index.html '.
The Pixel Perfect Camera component:
ensures your pixel art remains crisp and clear at different resolutions, and stable in motion.
- Layout Group with a Flexible Size: might allow the layout group to adapt to the screen size and resolution.
- Canvas scaler with a reference resolution: try setting the Scale Mode to Scale With Screen Size. Adjust the Reference Resolution to match your design resolution.
- Script to Adjust the Layout: detect params and account for them
- Pixel Density-Based Layout: a layout group that takes into account the pixel density of the screen
- Snap Elements to Whole Pixels: e.g. RectTransform rounding scripts to ensure elements are placed at whole-number coordinates
Please inform how it goes!
Set Canvas Scaler to these options:
UI Scale Mode = Scale With Screen Size
Match Height = 1.0