Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the same part of game on every aspect ratio on Android devices with Unity?

I'm coming from Libgdx engine to Unity and I'm a beginner programmer. I try to make a game in Unity for Android and iOS devices, but I have a problem with the aspect ratio/resolution scaling. In Libgdx if you have develop something you give a "default" resolution, and everything will be scaled to the device's screen, not matter if it has bigger or smaller resolution, and nothing will be cut off from the screen. On the other side, in Unity if I make a game it will crop the screen to the chosen aspect ratio, which is not the best if you want the same experience across all the devices available. I have searched on the internet for a long time, but I haven't find anything that would work for me.

Bottom line: I would like to know if there's some code for Unity (C#) which will make my game looks like the same across all devices with different aspect ratios? I'm developing with Unity 2D. If anybody could code it and would like to share it, I would be very happy, because it is very bad if it doesn't look like same on all devices. I don't have the problem with onGUI yet, as I haven't did any menu or something for it, just the game, but it is not good that the background is not the same on devices, and that the character is not on the same portion of the screen.

like image 799
Zwiebel Avatar asked Oct 21 '22 12:10

Zwiebel


1 Answers

During init, use Camera.pixelRect to determine the Height and Width of the Device assuming the camera is fullscreen.

Store the Height and Width and then do some math with your default settings. Eg. Default settings is 480x854, pixelRect is 800x1280. So difference in width is 800f/480f. Do the same for height.

Use the difference and multiply it to the object at the top of the hierarchy.

Hope it helps.

like image 110
KennethLJJ Avatar answered Oct 23 '22 03:10

KennethLJJ