Unity 2D - Scale/Resize Camera Based On Screen Resolution/Size

Unity 2D - Scale/Resize Camera Based On Screen Resolution/Size

If you are building a 2D game for devices with various screen sizes, the first thing you would need is to have the look and feel of your game, same across all the devices. To achieve this you would want to change the size of your Orthographic camera based on the screen height.

In short, this post lets you create a script which will help you resize the camera based on the screen size of the device the game is being played on.

Create a new C# script named ScalableCamera and attach it to the Main Camera in your scene. Open the script and add the below code to it:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScalableCamera : MonoBehaviour {
 
 // Use this for initialization
 void Start () {
  float TARGET_WIDTH = 960.0f;
  float TARGET_HEIGHT = 540.0f;
  int PIXELS_TO_UNITS = 30; // 1:1 ratio of pixels to units
  
  float desiredRatio = TARGET_WIDTH / TARGET_HEIGHT;
  float currentRatio = (float)Screen.width/(float)Screen.height;
  
  if(currentRatio >= desiredRatio)
  {
   // Our resolution has plenty of width, so we just need to use the height to determine the camera size
   Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS;
  }
  else
  {
   // Our camera needs to zoom out further than just fitting in the height of the image.
   // Determine how much bigger it needs to be, then apply that to our original algorithm.
   float differenceInSize = desiredRatio / currentRatio;
   Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS * differenceInSize;
  }
 }
}


Save the script and return to unity and test the scene. Vary the PIXEL_TO_UNITS variable value to get the desired screen size based on your sprites' PixelToUnits value. Make sure you stop the scene and then change the resolution for the code to update the Camera size as our code is in the Start method.

Unity 2D - Scale/Resize Camera Based On Screen Resolution/Size


See you around
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
    Blogger
    Facebook

19 comments:

  1. Such a nice content. one can also get information on camera from infoshutter

    ReplyDelete
  2. In Camera.main.orthographicSize = TARGET_HEIGHT / 4 / PIXELS_TO_UNITS;
    why do you divide by 4 and not by 2?

    ReplyDelete
  3. Nice post, thanks. This website contains information on how to hack facebook account. Recommend visiting.

    ReplyDelete
  4. Rousing and particularly significant, significantly recommended for all bloggers out there.
    Rogue buddies 2 unblocked

    ReplyDelete
  5. In laymen terms image quality is how much detail is in the image, the lower the quality the less detail there is, if the quality is too low then the image looks blocky, if the quality is too high then the file size grows and this really matters if you are emailing or showing the image on the web.
    Thumbnail

    ReplyDelete
  6. I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. best buy dash cams

    ReplyDelete
  7. These days a camera seems to come as a free accessory to just about every electronic device available and it would see that just about everyone on the planet must have one. camera instructions

    ReplyDelete
  8. More and more interesting and also the new versions are being added to give the users more of entertainment Read more about league

    ReplyDelete
  9. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info  vlogging camera review

    ReplyDelete
  10. While it is true that in gaming merchant account, much like mainstream gambling, the outcome of the game is pretty much left to chance and luck,

    ReplyDelete
  11. more info a certain amount of research is necessary for some of the games. And some of the games,

    ReplyDelete
  12. Immediately that awesome web page really should irrefutably grown into known in the many webpage the majority of people, a result of the diligent articles or blog posts or maybe opinions or possibly responses. endzone camera

    ReplyDelete
  13. I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. blog for RC crawlers

    ReplyDelete
  14. Puzzles genre of digital games involves problem solving and analysis with varying degrees of difficulty depending on the nature of the game. SlopeUnblock.Weebly.com

    ReplyDelete
  15. Currently, the most commonly used hard drive interface used today is Advanced Technology Attachment (ATA); which comes in two forms, the original Parallel ATA (PATA) and the newer and faster Serial ATA (SATA).Best Pickleball Paddles

    ReplyDelete
  16. Unity 2D - Scale very well. I enjoyed this app. Thank you for sharing this great app.
    duck life

    ReplyDelete
  17. Gamers will appreciate the visually stimulating graphics that this game offer. Pick up your copy when it comes out in March of 2010.
    https://appmirror.net/

    ReplyDelete
  18. This article was written by a real thinking writer without a doubt. I agree many of the with the solid points made by the writer. I’ll be back day in and day for further new updates. digital camera

    ReplyDelete