Search Unity

android resolutions

Discussion in '2D' started by skakac-33, Nov 18, 2013.

  1. skakac-33

    skakac-33

    Joined:
    Oct 6, 2013
    Posts:
    8
    I searched but i didn't found the answer.

    Before 4.3 update I used Andengine to make games, and their handling resolutions was simple.
    I set resolution 480x800, make the game for 480x800 screen, and andengine does scaling based on ratio (eg 4:3, 16:9).

    Here in unity I can't set resolution, and scaling is auto (yes i can set camera.aspect = 4f/3f).
    If I make new scene, set 480x800 image in scene and run it (to my android phone, it has 480x800 res), I get: http://i.imgur.com/bpTdRVE.png

    What should I do?
     
  2. boolean01

    boolean01

    Joined:
    Nov 16, 2013
    Posts:
    92
    I've hit the same wall. I'm using libGDX right now and once I put in 480x800 the game will always scale to fit the device (for better or worse). With Unity I can get my game to fit nicely in a 480x800 screen, but any larger resolutions just add extra space like your image.
     
  3. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    What's the size of your orthographic camera? Try setting it to 4 (Target Screen Height / 2 / Pixels to Units Ratio).
     
  4. boolean01

    boolean01

    Joined:
    Nov 16, 2013
    Posts:
    92
    Hi bryantdrewjones

    I have it set to 4 at the moment and that works fine for 480x800. The problem is when I then put it on something like a Nexus4 which has a resolution of 768x1280 - I change the orth size to 6.4 but instead of the game stretching to fill the screen I just get extra blank space like skakac.33.

    From what I can tell, this might just be how Unity works since it's running on a 3D engine? It only understands aspect ratios, not per-pixel scaling?
     
  5. Mogitu

    Mogitu

    Joined:
    Nov 13, 2013
    Posts:
    40
    Im developing my game with 320*480 as target resolution.
    For this i set up my camera size as 15 because i want 32 pixels to be 1 unit(480/32=15). The size mainly represents the height variable.

    To make this fill all screens properly(filling it completely) i calculated the height that fits my desired screen width.
    Basically:

    float desiredHeight=(targetHeight/targetWidth)*Screen.Width.
    Screen.SetResolution(Screen.Width, (int)desiredHeight,true);

    There might be a better solution which i havent found yet, but for now this fits my use case and works well on several screen sizes. Maybe this approach helps you to.
     
  6. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  7. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    Ahh, if your assets are designed for 480 x 800, you should keep your orthographic size at 4 regardless of the actual screen height of the device. I'm guessing you have a script that changes like orthographic size at runtime depending on the device's resolution? Locking the size at 4 should keep the game looking consistent across all possible resolutions.
     
  8. boolean01

    boolean01

    Joined:
    Nov 16, 2013
    Posts:
    92
    You're a champion! That seems to have fixed it for me! Skakac does this work for you too?

    So if I understand correctly, the size of the orthographic camera should stay the same size as my asset scale (4), and because my assets are not actually changing in size neither should my ortho camera. But I increase or decrease the resolution of the screen it will still always gets constrained to the size of my orthographic camera, giving the impression of it scaling up or down.

    Is that...right? :???:
     
  9. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    Yes, exactly :) It's confusing stuff, especially if you're used to working with pixels instead of Unity's arbitrary world units :confused:
     
  10. kieranahayes

    kieranahayes

    Joined:
    Mar 26, 2013
    Posts:
    3
    I've been stuck with the same problem, I believe, and the solution does not seem to work correctly for me. It seems to work to bind the top and bottom of my asset (a 640x960 png) to whatever resolution I'm in, which is okay most of the time. But if the width of the device becomes too narrow than the sides are cut off.

    For instance, 800x480 is good: http://imgur.com/Y055j5Y ..

    But if I switch to 480x800: http://imgur.com/vyzpCVS ..you'll see the top and bottom are aligned with the edge of the screen at the cost of the edges of my gameboard. In this case I would like the camera to line up with the left and right side of the board, even though it will add some extra space above and below the image. In this case I can change my camera size to ~550 and it will fit perfectly around the image. I'm just not sure where this 550 comes from or how I should calculate it.

    I would think it a pretty common request to have a static camera that would fit a game board as tight to the edges as possible, though I cannot seem to find a known method. Any help is appreciated.
     
    Last edited: Nov 21, 2013
  11. Mogitu

    Mogitu

    Joined:
    Nov 13, 2013
    Posts:
    40
    the camera size mainly represents the height and doesnt factor the width as consistent, you will need to do some calculations to make it fit your entire screen. might wanna try my solution i posted earlier in this topic.
     
  12. kieranahayes

    kieranahayes

    Joined:
    Mar 26, 2013
    Posts:
    3
    Thanks Mogitu.

    After taking a look at your solution, I realized it wouldn't quite work for me (you seem to be setting the resolution, which I have no control over on a mobile device). But it did get me thinking about the relationship between the width and height, and how I could use that to determine the size of my camera and I believe I have come up with a solution.

    Because my target resolution is 640x960 (and my pixels to units ratio is 1), I determined my "desired ratio" of width to height by dividing 640 by 960 (.666). Now when the game starts up the first thing I do on the camera is determine the current screen's ratio of width to height. If the current ratio is GREATER than .666 then we will have extra space to the left and right of the game board, which is handled perfectly by our current camera with a size of 480 (Target Screen Height / 2 / Pixels to Units Ratio). Now if the current ratio is LESS than .666, then our screen resolution is too narrow width-wise and the left and right of our board will be cut off. To fix this we need to zoom out on the camera, AKA increase the size of the camera to include some extra space above and below our game board so that our width will fit in frame.

    To determine how much we needed to increase the size, I simply determined how much our current ratio was off of our desired ratio, and scaled the camera size by that amount: Camera.main.orthographicSize = (Target Screen Height / 2 / Pixels to Units Ratio) * (desiredRatio / currentRatio)

    Here is the script I attached to the camera to make this happen:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraSetup : MonoBehaviour {
    5.  
    6.      // Use this for initialization
    7.      void Start () {
    8.           float TARGET_WIDTH = 640.0f;
    9.           float TARGET_HEIGHT = 960.0f;
    10.           int PIXELS_TO_UNITS = 1; // 1:1 ratio of pixels to units
    11.  
    12.           float desiredRatio = TARGET_WIDTH / TARGET_HEIGHT;
    13.           float currentRatio = (float)Screen.width/(float)Screen.height;
    14.  
    15.           if(currentRatio >= desiredRatio)
    16.           {
    17.                // Our resolution has plenty of width, so we just need to use the height to determine the camera size
    18.                Camera.main.orthographicSize = TARGET_HEIGHT / 2 / PIXELS_TO_UNITS;
    19.           }
    20.           else
    21.           {
    22.                // Our camera needs to zoom out further than just fitting in the height of the image.
    23.                // Determine how much bigger it needs to be, then apply that to our original algorithm.
    24.                float differenceInSize = desiredRatio / currentRatio;
    25.                Camera.main.orthographicSize = TARGET_HEIGHT / 2 / PIXELS_TO_UNITS * differenceInSize;
    26.           }
    27.      }
    28.  
    29.      // Update is called once per frame
    30.      void Update () {
    31.      }
    32. }
    This seems to work to fit my game board to the contents of any resolution without cutting off anything vertically or horizontally. Thanks for the help!
     
    elpuerco63 likes this.
  13. Mogitu

    Mogitu

    Joined:
    Nov 13, 2013
    Posts:
    40
    Ah, cool you got it to work like that! Though i use my solution for mobile devices, which works pretty good so far. But i see now that it doesnt fit your use case. My solution will stretch the image just for the sake of filling the entire screen(no extra space horizontall/vertically).
     
  14. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    That, from my tests, works a treat. Thanks man.
     
  15. Saxi

    Saxi

    Joined:
    Jun 28, 2013
    Posts:
    381
    Have you considered just using camera.aspect?
    That would eliminate some of your code.
     
  16. amjadyahya

    amjadyahya

    Joined:
    Nov 16, 2013
    Posts:
    11
  17. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    I was looking for a solution to this problem too, I think that your code made possible for me to solve my issue.
    Would you mind if I use it for the release version of my game? I will put your name in the credits if you want.
     
  18. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122

    which would be desired ratio in this case to remove the division (desiredRatio / currentRatio)
     
  19. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Hero....this solved the problem for me, thanks ;-)