- Home /
Is there a way to scale the 2:3 resolution to the 480:800?
So I'm trying to develop a game for android and I didn't look into screen resolution before I started making the game. This is my first time making a game and even my first time using Unity and so I didn't really think about it but now it's a huge problem.
I developed all of my sprites and my game around the 3:2 (2:3) resolution and everything is fine and dandy on my computer. But then, I port it to my android device and everything is getting cropped off on the edges (top, bottom, left, and right). Fair enough, change the resolution to 480:800 because apparently that's what android phones are mostly compatible with. So I change the resolution but when I do that, the cropping is happening on my computer as well. So my guess is that when I port the game, it automatically changes the resolution of my game to 480:800 or whatever. I try porting it to my android device a second time and the cropping still occurs like I guessed it would.
So now what? Would I able to use a script that would be able to scale everything to what it needs to be? I tried scaling every gameobject to 90% so that it would be the right size for the 480:800 resolution but that ends up making a few weird things happen like some of the gameobjects appear when they shouldn't.
Any advice would be appreciated. Thank you.
I've found that scales the screen. The code is:
void Start() { // set the desired aspect ratio (the values in this example are // hard-coded for 16:9, but you could make them into public // variables ins$$anonymous$$d so you can set them at design time) float targetaspect = 2.0f / 3.0f;
// deter$$anonymous$$e the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
// obtain camera component so we can modify its viewport
Camera camera = GetComponent<Camera>();
// if scaled height is less than current height, add letterbox
if (scaleheight < 1.0f)
{
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
camera.rect = rect;
}
else // add pillarbox
{
float scalewidth = 1.0f / scaleheight;
Rect rect = camera.rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
camera.rect = rect;
}
}
This fixes the problem but now I have a problem with the Unity UI in which is, it doesn't actually scale with the screen size in the code. I'm not familiar with the UI or screen resolution so I'm not sure how to script for the to work together.
Any advice would be appreciated. Thank you.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Q about Building my game for so many devices 2 Answers
Unity Android fetching wrong screen resolution 1 Answer
Distribute terrain in zones 3 Answers
Developing on multiple resolutions 1 Answer