Search Unity

C# scale objects to fit the screen

Discussion in 'Scripting' started by agostonr, Jul 26, 2016.

  1. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Hi all,
    I have a couple of objects, could arrange them in a rectangular shape. I wish to set the scale and the distance between them so it fits the mobile screen. Shall I use the Screen.width and height or what exactly?
    I'm arranging sprites whose size is 512X512 pixels each, uniform size they have.
    I'm a bit confused because I do not know how to go between screen width and height and the scale of a couple of objects.
    Thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    There are several phases of this to understand.

    The basic output of pixels for your mobile device is (Screen.width, Screen.height).

    If you have an orthographic camera, the world height of your viewport will be your camera's .orthographicSize value times 2, and the width will be adjusted proportionately by the Screen.width / Screen.height ratio above.

    If you are working on a UI Canvas, the units you use will vary between the Canvas's RenderMode, and also the CanvasScaler's UI Scale Mode, both of which interact with each other depending on what you set them to.

    The world size of your sprite will be the pixel size divided by the "Pixels Per Unit" on the import setting, or else on the fourth argument to pass to Sprite.Create(), if you create it programmatically.

    I recommend you start here:

    - make you camera Orthographic
    - set its Size field to 5 (should already be that)
    - drag your 512x512 pixel sprite into the scene, centered in front of the camera (same X,Y)

    Note that it takes approximately half the height of the screen. It actually takes precisely 512 / 1000 of the height of the screen, as follows:

    512 x 512 sprite with 100 pixel import is 5.12 x 5.12 world size
    Ortho size of 5 means screen height of 10

    Therefore height of sprite should be 5.12 / 10 of screen.

    Start there and fiddle with the different settings until you develop an intuitive feeling for how they all interact. Then you can go onto tinkering with settings in a UI Canvas.

    I recommend NOT changing the pixels per unit import setting, because it will become a confounding factor in your project if every sprite has a different import setting.
     
    agostonr likes this.
  3. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks, I think I got it now. I have a real size of 5.12, and the full is 10. so I need to scale it up, 10/5.12 = 1,953125, giving it this scale one card fits the height.
    If I need to line up 10 cards below each other to fit, I need to give them a scale of 0.1*1,953125 then.

    Another thing, I will force the game to portrait mode. In that case the height will be the obvious height, so the longer side, or it changes between landscape and portrait?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The camera orthoSize drives the height of the camera viewport, regardless of portrait/landscape. Therefore if you have a square sprite and scale it to full height, a lot of the left and right will be cut off in portrait mode.

    From your reply post it sounds like you have an excellent handle on how things are working and the math behind it all.
     
  5. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks for the input again.
    I figured I won't scale them one by one, but parent under another game object, store the total width and height of the collection (without scale it is 5.12*how many do I have) and scale that after everything is set (but before the camera shows us what's happening and the player could interact).