Hello!
I am building a little mobile game and I need some help with the automatic resizing of the resolution for the different screen sizes. I did research and found using a matrix to resize the GUI aspects of the game was best and it works great. My problem comes from, I am guessing, the camera view itself.
I am currently using this script which I thought would fix it but it doesnt:
public class ControllingCameraAspectScript : MonoBehaviour
{
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 instead so you can set them at design time)
float targetaspect = 16.0f / 10.0f;
// determine 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;
}
}
}
While it works perfect for my tablet, when I try it on my phone I can actually see different scenes behind my game screen. It’s like my menu scenes are the full screen width, but when I load my main game scene it isn’t as wide/full screen and you can see the edges of my menu scene. So I guess my question is, how can I get my main scene to go full screen? Any idea how to fix this?
I am fairly new with programming so any help will be greatly appreciated!