• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OJ3D · Aug 04, 2015 at 04:30 AM · cameraaspect-ratioscreen resolutionscreen sizetablet

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Scale Everything In Game to Fit all screen Sizes 1 Answer

How do I negate the zoom caused by changing resolution and screen orientation? 1 Answer

How to make my UI items/canvas stretch to fill preset aspect ratio 0 Answers

Stop Camera Size and GUI Position Displaying Differently on Different Resolutions 1 Answer

View fits in landscape but not portrait ?! (Mobile) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges