Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
8
Question by brag42 · Jan 16, 2014 at 06:17 AM · androidiosmobiledeviceaspect ratio

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?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
46
Best Answer

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;
         }
     }
  
 }
Comment
Add comment · Show 14 · Share
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
avatar image Tillrobby · Jul 12, 2014 at 12:51 PM 0
Share

This works on Android too! Thank you so much, this fixed the resolution problems I was having on Androids.

avatar image Mikenekro · Aug 11, 2014 at 06:36 PM 0
Share

Great and simple solution to the problem of screen sizes!

avatar image hari_unity · Oct 20, 2014 at 06:06 AM 0
Share

Thanks but am getting error at camera.rect = rect;, where there is no property called rect for camera

avatar image robertbu · Oct 20, 2014 at 06:08 AM 0
Share
@hari_unity - the above code compiles just fine for me, so I'm not sure if your issue. If you cannot figure it out, post a new question.
avatar image Leumasual · Feb 25, 2015 at 12:53 PM 1
Share

Screenshot

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.

Show more comments
avatar image
2

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;
         }
     }
     
 }
 
Comment
Add comment · Share
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
avatar image
0

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:

https://www.assetstore.unity3d.com/en/#!/content/27922

Comment
Add comment · Share
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
avatar image
0

Answer by Jhonatas.C · Dec 21, 2015 at 03:06 AM

can someone update the code?

Comment
Add comment · Share
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
avatar image
0

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 ???

Comment
Add comment · Show 1 · Share
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
avatar image siddharth3322 · Oct 12, 2017 at 10:08 AM 0
Share

Above code is for desktop games, in mobile device this code will not work.

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.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mobile aspect ratio and scaling - Use 16:10 or 16:9 base for full screen background ? 0 Answers

Mobile - Check for headphones 0 Answers

Android/iOS notifications when the game is not running 1 Answer

Is this a good practice for mobile devices? 2 Answers

How to detect if the device is android mobile or tablet 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges