• 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 Rodney26 · Jun 25, 2015 at 06:35 PM · camerascaleaspect-ratioscreen resolution

Scale Everything In Game to Fit all screen Sizes

I have checked online both on for the answers to this question but have only found vague responses, i would be so grateful if someone could help me out with this.

I am creating a small game, and am near completion, i used the Unity build settings to build the game but then came across a problem. the game was designed in the 16:10 aspect ratio as it suits my monitor, but anytime i build the game on a smaller resolution parts of the game get cut off from the screen. I intended to release this game for android and obviously need something that allows the game to fit on any size screen or tablet.

the GUI scales fine due to the option to make the canvas 'scale with screen size' im looking for the same for everything else in the game.

additional info:

I have an orthographic camera

I have been told in other forums that i need Camera.main.aspect = ScreenWidth / ScreenHeight;

but in my script this doesnt do anything.

I have included my camera script as well:


public class CameraScript : MonoBehaviour {

 public Transform PlayerTarget;

 public float YaXis;
 public float XaXis;
 public float moveDown;
 public float moveUp;
 
 public Vector2 
     margin, 
     Smoothing;
 
 private Vector3 
     _min, 
     _max,
     PlayerPosition;
 
 
 public bool IsFollowing;
 
 public BoxCollider2D CamBounds;
 
 public void Start()
 {
 

     //float worldScreenHeight = Camera.main.orthographicSize * 2f;
     //float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
 
     //Camera.main.aspect = worldScreenWidth / worldScreenHeight;

     _min = CamBounds.bounds.min;
     _max = CamBounds.bounds.max;
     IsFollowing = true;
     
 }
 
 public void Update()
 {
     var x = transform.position.x;
     var y = transform.position.y;
     
     if (IsFollowing) 
     {
         if (Mathf.Abs(x - PlayerTarget.position.x)> margin.x)
             x = Mathf.Lerp (x, PlayerTarget.position.x, Smoothing.x * Time.deltaTime);
         
         if (Mathf.Abs(y - PlayerTarget.position.y)> margin.y)
             y = Mathf.Lerp (y, PlayerTarget.position.y, Smoothing.y * Time.deltaTime);
     }



     var CameraHalfWidth = GetComponent<Camera>().orthographicSize * ((float)Screen.width / Screen.height);

     x = Mathf.Clamp (x, _min.x + CameraHalfWidth, _max.x - CameraHalfWidth);
     y = Mathf.Clamp (y, _min.y + GetComponent<Camera>().orthographicSize, _max.y - GetComponent<Camera>().orthographicSize);


     if (Input.GetKey(KeyCode.DownArrow)){
         transform.position = new Vector3 ((x - XaXis) , (y + YaXis) - moveDown, transform.position.z);
     }else if (Input.GetKey(KeyCode.UpArrow)){
         transform.position = new Vector3 ((x - XaXis) , (y + YaXis) + moveUp, transform.position.z);
     }else
     transform.position = new Vector3 ((x - XaXis) , (y + YaXis), transform.position.z);

 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RayJr · Jun 25, 2015 at 07:27 PM

You can force a screen ratio in your game. If someone is using a different ratio, they will see black bars on the top and bottom or sides to meet the ratio you defined..

Screen.SetResolution

Comment
Add comment · Show 2 · 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 Rodney26 · Jun 25, 2015 at 08:34 PM 0
Share

not really sure thats the way i want to go, it may seem a little unprofessional, especially for the amount of different devices run on android, alot of people will be playing it with black bars, but appreicate you answering as if its something new i have now learnt :)

avatar image RayJr · Jun 25, 2015 at 08:40 PM 0
Share

I hear ya. The most common way to handle this is to test in various resolutions as you build.

Though, you're not forcing the resolution, just the ratio. the black bars may be quite small in many cases.

Good Luck!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Aquarium like camera 1 Answer

Camera Fit Issues on 10 inch tablet 0 Answers

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

How to fit a background sprite with 4:3 aspect ratio in a 16:9 screen by simply making the extra content of the sprite from the bottom go outside the camera? 2 Answers

What would be the appropriate take on making the camera retain the horizontal space despite the aspect ratio 1 Answer

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