- Home /
Camera Fit Issues on 10 inch tablet
How's it going everyone. I'm currently designing a 2D game that should be out for Android Soon. I've tested my app on my Samsung galaxy s3, nexus 7in tablet, and samsung 10in tablet, in addition to testing within the Unity emulator. Everything looks great, except when I play in my 10in tablet.
The aspect ratio for the 10 in tablet is 16:10. My game is only played in portrait mode. It seems to get a grip on the height but crops the width. Therefore the camera trims the edges about half an inch on both sides. So odd because technically the nexus 7 is the same aspect ratio.
I'd really appreciate the feedback. My code is attached ( I compiled it from a Camera Fit script I found and added some of my touches).
///////////////////////////////////////////////////////////////
using UnityEngine; using System.Collections;
public class ControllingCameraAspectScript : MonoBehaviour { //We're going to use this at a public level
private float AspectRatioWidth;
private float AspectRatioHeight;
private float ScreenAspectRatio;
public bool WeHit16_9Here;
public bool WeHit16_10Here;
public bool WeHit17_10Here;
public bool WeHit5_3Here;
public bool WeHit4_3Here;
public bool WeHit3_2Here;
// Use this for initialization
void Start()
{
//Auto Set TargetAspect
//Read the Device's Screen Aspect Ratio
//and Automatically Set the Desired Aspect Ratio
//So we don't have to hardcode it
//Calculate Aspect Ratio
ScreenAspectRatio = (float)Screen.height / (float)Screen.width;
print ("The ScreenAspectRation Calc is : " + ScreenAspectRatio);
//Below is for Portrait Mode
//To apply for Widescreen just flip the widths&Heights
//Set 16:9 Aspect Ratio -- Portrait Mode Set (9:16)---1:1.78
if (ScreenAspectRatio >= 1.76f && ScreenAspectRatio <= 1.80f) {
AspectRatioWidth = 9f;
AspectRatioHeight = 16f;
print ("We hit 16:9");
WeHit16_9Here = true;
}
//Set 5:3 Aspect Ratio -- Portrait Mode Set (3:5)---1:1.67
else if (ScreenAspectRatio >= 1.65f && ScreenAspectRatio <= 1.69) {
AspectRatioWidth = 3f;
AspectRatioHeight = 5f;
print ("We hit 5:3");
WeHit5_3Here = true;
}
//Set 17:10 Aspect Ratio -- Portrait Mode Set (10:17)---1:1.71
else if (ScreenAspectRatio >= 1.69f && ScreenAspectRatio <= 1.73f) {
AspectRatioWidth = 10f;
AspectRatioHeight = 17f;
print ("We hit 17:10");
WeHit17_10Here = true;
}
//Set 16:10 Aspect Ratio -- Portrait Mode Set (10:16)---1:1.60
else if (ScreenAspectRatio >= 1.58f && ScreenAspectRatio <= 1.62f) {
AspectRatioWidth = 10f;
AspectRatioHeight = 16f;
print ("We hit 16:10");
WeHit16_10Here = true;
}
//Set 3:2 Aspect Ratio -- Portrait Mode Set (2:3)---1:1.50
else if (ScreenAspectRatio >= 1.48f && ScreenAspectRatio <= 1.52f) {
AspectRatioWidth = 2f;
AspectRatioHeight = 3f;
print ("We hit 3:2");
WeHit3_2Here = true;
}
//Set 4:3 Aspect Ratio -- Portrait Mode Set (3:4)---1:1.33
else if (ScreenAspectRatio >= 1.31f && ScreenAspectRatio <= 1.35f) {
AspectRatioWidth = 3f;
AspectRatioHeight = 4f;
print ("We hit 4:3");
WeHit4_3Here = true;
} else { //None of the Conditions Were met apply a standard Aspect Ratio 16:9 Portrait (9:16)
AspectRatioWidth = 9f;
AspectRatioHeight = 16f;
print ("We hit 16:9");
WeHit16_9Here = true;
}
// 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 = 9.0f / 16.0f; //Hardcoded Method for Portrait Mode
//In General it's really targetaspect = 16.0f/9.0f for traditonal readings
float targetaspect = AspectRatioWidth/AspectRatioHeight;
// 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;
//Print
print ("The WindowAspect :" + windowaspect);
print("Screen Width :" + (float)Screen.width);
print ("Screen Height :" + (float)Screen.height);
print ("Aspect Ratio :" + targetaspect);
print ("The Screen Resoloution is : " + Screen.currentResolution);
print ("Scaleheight is :" + scaleheight);
// 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;
}
}
}//END OF MAIN