- Home /
Mobile device screen sizes
Can I develop for the standard iPhone/Mini/iPad screen sizes at once? Or do I need to make some change and build for each one separately?
Is it just a matter of aspect ratio or is there more I need to know?
Answer by siddharth3322 · Jan 17, 2014 at 07:57 AM
I think following code snippet work for you. Because it satisfy my needs nicely. You have to attach this script to your camera object.
public class ControllingCameraAspectScript : MonoBehaviour
{
// Use this for initialization
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 / 9.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;
}
}
}
This works on Android too! Thank you so much, this fixed the resolution problems I was having on Androids.
Great and simple solution to the problem of screen sizes!
Thanks but am getting error at camera.rect = rect;, where there is no property called rect for camera
Any idea why my screen looks like this? It's fine with Apple devices and other Android devices. Only $$anonymous$$e. $$anonymous$$y guess is something to do with IF the device aspect ratio is the same as the target aspect ratio
I messed around with it and I realise I wont get the problem if the camera's view is way larger than the game area. $$anonymous$$eaning I doubled the size of the camera.
Answer by Omnikam · Apr 29, 2016 at 06:29 AM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ImageVideoContactPicker;
public class ControllingCameraAspectScript : MonoBehaviour {
// Use this for initialization
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 / 9.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;
}
}
}
Answer by Ultra Assets 4 You · Feb 18, 2015 at 08:45 PM
Hi. The best solution for me is to use the theorem of intersecting lines so that there is neither a cut off on the sides nor a distortion auf the game view. That means that you have to step back or forward in dependency of the different aspect ratio.
If you like, I have an asset on the unity asset store which automatically corrects the camera distance so you never have a distortion or a cut off no matter which handheld device you are using. it is available here:
Answer by KhizarRaza · Oct 12, 2017 at 10:02 AM
Hello plzzz help me i used this code but it never works on camera i add this script in my main camera ..... Answer me plzzzzz.... @ siddharth3322 is this script need to add only for one camera or every camera in different scenes ???
Above code is for desktop games, in mobile device this code will not work.