Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Screen size measurement problem...

Discussion in '2D' started by rabirland, Nov 25, 2014.

  1. rabirland

    rabirland

    Joined:
    Nov 22, 2014
    Posts:
    29
    In my game i need to re-size the character (which is a gameObject with a sprite renderer) to a specific size, which is excatly the 1/7 of the screen...
    I use this code:


    Code (CSharp):
    1. float ScaleIndex = 1f;
    2. float CharSize = Player.renderer.bounds.size.x * 100; //Sprite Unit is set to 100
    3. float PreferredSize = Screen.height / 7;
    4. StaticStorage.ScaleIndex = PreferredSize / CharSize;
    5.  
    6. Player.transform.localScale = new Vector3(StaticStorage.ScaleIndex, StaticStorage.ScaleIndex);
    My problem is, that the game resize the character to everything but the 1/7 ofthe screen...

    Examples:
    Unity editor:
    Render Area: 15,4 Cm
    Player Size: 1,3 Cm
    15,4 / 1,3 = 11,8 (which is not even close to 7...)

    External windows game window size (my monitor size in fullscreen)
    Window size: 27,1 Cm
    Player Size = 4,0 Cm
    27,1 / 4,0 = 6,85 (Which is yes, pretty close to 7)

    Tablet:
    Screen size: 11,3 Cm (Statusbar not counted in, i don't know if Unity count it or not, but that doesn't matter with that diference)
    Player Size: 1,2
    11,3 / 1,2 = 9,41 (Which is again pretty far from 7)

    And the most fun, my WT19i Android phone:
    Screen size, without statusbar: 4,55 Cm
    Player Size: 0,2 Cm
    4,55 / 0,2 = 22.75 Cm (Whaaaaaaaaaaaat ???)

    What is wrong with it ?
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Check the your camera size:


    PIxel PerfectCamera
    Orthographic camera size = Screen.height / pixels to units / 2;

    With your Orthographic camera set right for your pixels to units, try this:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Resize : MonoBehaviour
    6. {
    7.     public float denominator = 2f;
    8.     public float ppu = 100f;
    9.  
    10.     float wantedRatio;
    11.     float screenHeight;
    12.     Vector2 characterOriginalSizePx;
    13.  
    14.     //Orthographic camera size =  Screen.height / pixels to units / 2;
    15.     void Start () {
    16.         screenHeight = Screen.height;
    17.      
    18.         characterOriginalSizePx = new Vector2(transform.renderer.bounds.size.y, transform.renderer.bounds.size.x) * ppu;
    19.         wantedRatio = screenHeight/denominator;
    20.      
    21.         Vector2 newSize = new Vector2(1, 1) * wantedRatio;
    22.         transform.localScale = newSize/ppu;
    23.     }
    24.  
    25. }
    26.  
     
  3. rabirland

    rabirland

    Joined:
    Nov 22, 2014
    Posts:
    29
    So my code looks like:

    Code (CSharp):
    1. float ScaleIndex = 1f;
    2.  
    3. float WantedRatio = Screen.height / ScaleByScreenRatio; //(Screen.height / 2f)
    4. ScaleIndex = WantedRatio / 100f;
    5.  
    6. StaticStorage.ScaleIndex = ScaleIndex;
    7.  
    8.  
    9. Player.transform.localScale = new Vector3 (StaticStorage.ScaleIndex, StaticStorage.ScaleIndex);
    And a screenshot:


    Its clear the the red cube is not the sized for the half of the screen...

    Edit:
    The measuring works well on PC, but on android, which would be my target platform, i get full random player sizes... I set it t half of the screen, on PC it works well, but on my tablet its somewhere about 1/3 of the screen, and on my phone its even smaller...
     
    Last edited: Nov 25, 2014
  4. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    I think this could be, because the Pixel density is different on PC (72 pixel per inch) than on some mobile.

    What is your tablet and Phone?
     
  5. rabirland

    rabirland

    Joined:
    Nov 22, 2014
    Posts:
    29
    I already fixed the problem... But since its a ratio (Screen.Height / 2) it should be the same for all density...
     
  6. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    How did you fix? Please share, in case someone else has that issue. ;)