Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Unity 2D: How to set the camera (screen) and game width to be the same and scale properly?

Discussion in 'Scripting' started by stalyan77, Jan 18, 2015.

  1. stalyan77

    stalyan77

    Joined:
    Nov 24, 2013
    Posts:
    14
    First of all, i'm new to Unity scripting and my head is going to explode, so forgive and correct as you see fit any mistakes I made here. =)

    I've been searching a lot and still get confused.
    My goal is changing the width of the main camera so it's at the same resolution than when playing fullscreen or hitting the Play button. Let's say I have a background of 512x256 which I want to be taken as reference for a full camera and screen resolution. I imported it as object set to 100 Pixels to Units (this would be 5.12 x 2.56 World units, right?). I have the main camera size at 1.28, which is half the height (256/2), so this should be right. Now the idea is the screen, editor and game resolution to be the same for the WIDTH.

    But first, how to set the camera width properly so it encloses the 512 pixels of the background?. I've read this depends on the aspect ratio.

    The aspect ratio is width divided by height, so in my example case:
    Aspect ratio = 512/256 = 2.

    Then, what I do this this number?

    camera.pixelWidth is read only, so I can't modify the width this way...

    http://docs.unity3d.com/ScriptReference/Camera-rect.html
    it seems camera rect it's only used to show a portion of the screen (modifying only the viewport if i'm not mistaken), being the maxium x value 1, that would be the full width... so this won't allow me to increase and display the intended screen width size in the viewport, editor and game...

    http://docs.unity3d.com/ScriptReference/Screen.SetResolution.html
    I attacheded a small script with this one in the camera to set it at 512x256 but i can't see any change neither in the viewport nor when i hit play, it's all just zoomed in (even when creating a 512x256 resolution in the Game tab dropdown menu).

    EDIT: I could get the camera width to fit perfectly to 512 pixels if I set the Viewport Rect W to 1.5 in the Inspector, but I don't know why this number works. Even though, When I hit Play, the screen is zoomed in and doesn't display the full background.

    So... i can't figure out:

    - Why setting Viewport Rect W to 1.5 worked?
    - How to show the full background when I hit play?


    Some related bonus questions:

    - Would i need to use "camera.aspect" to set a proper aspect ratio for the width and height if I want the game objects to display properly on mobile devices?

    - How does the current monitor resolution affects to what the Editor, viewport and Game tab display?

    - If I play a 512x256 game on a 1024x768 resolution, I understand the aspect ratio will change so the game object will look distorted. Any way to mitigate this so it looks okey?

    Thanks for reading until this point and I would appreciate if only you ask one of this questions =)
     
  2. stalyan77

    stalyan77

    Joined:
    Nov 24, 2013
    Posts:
    14
  3. stalyan77

    stalyan77

    Joined:
    Nov 24, 2013
    Posts:
    14
    Okey, the solution was simple but it's not that apparent, so for anyone struggling with setting the width/height on the editor and then visualizing his game at the same resolution in the Game Tab, here the steps:

    - Import an image to Unity with the intended height/width (a background or whatever)
    - Set the Pixels per Unit in the Inspector setting with value 1.
    - Change the Texture type to Sprite.
    - Select the main camera.
    - If your intended resolution is for example 512x256 (width/height), get the half of the height, that's 128.
    - On Size, enter 128. You'll probably see that the width of the camera viewport doesn't fit properly with the 512x256 image (normally not displaying a chunk of the image at both sides).
    - Click on the Game tab.
    - On the Top Left, click on the dropdown menu displaying the screen resolution.
    - Click on the + sign at the botton of the dropdown menu.
    - Type your width & height (512x256 in our example)
    - Set the Type to aspect ratio.
    - Select the resolution that you created.
    - Go back to the Scene. Voilà! now the camera shows the reference image properly.

    iMPORTANT: My mistake was assuming that 100 Pixels per unit when importing the image was the best because Unity said so by default, neither i could relate the fact that to change the width in the camera screen in the Editor I needed to change the resolution in the Game Tab first, which seems very odd. (could this be a BUG or is the natural workflow?)

    If this helps someone i'll be content enough =)
     
    Last edited: Jan 18, 2015
  4. Hardcore_Graverobber

    Hardcore_Graverobber

    Joined:
    Jul 31, 2016
    Posts:
    9
    Hello,

    this helped me more than 3 years later :D but in fact you don't have to set the Pixel per unit to 1.
    All you have to do is to divide the camera size by the value of the pixels per unit.
    For example if you kept the initial 100 it will be 128/100 = 1.28 and it should fit

    P.S. if you're too lazy to make the calculation yourself you could even litterally type it into the size field and hit enter, Unity will calculate it for you
     
    Awufumana likes this.
  5. jasoncoons

    jasoncoons

    Joined:
    Nov 30, 2017
    Posts:
    1
    Very helpful & clear!
     
  6. exceptionthrown

    exceptionthrown

    Joined:
    Apr 20, 2020
    Posts:
    1
    5 years later and this was very helpful!
     
  7. laukikm11

    laukikm11

    Joined:
    Sep 8, 2020
    Posts:
    1
    Thanks for this. I'd like to add a few things.
    1. The viewport rect only affects the camera preview and doesn't affect the game display itself
    2. If you're using screencapture, the screenshot is the same size as the display. And if you don't plan to make an interactive game, just generate datasets, changing the display size is enough