Screen size measurement problem...

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:

float ScaleIndex = 1f;
float CharSize = Player.renderer.bounds.size.x * 100; //Sprite Unit is set to 100
float PreferredSize = Screen.height / 7;
StaticStorage.ScaleIndex = PreferredSize / CharSize;

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 ?

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:

using UnityEngine;
using System.Collections;

public class Resize : MonoBehaviour
{
    public float denominator = 2f;
    public float ppu = 100f;
  
    float wantedRatio;
    float screenHeight;
    Vector2 characterOriginalSizePx;
  
    //Orthographic camera size =  Screen.height / pixels to units / 2;
    void Start () {
        screenHeight = Screen.height;
      
        characterOriginalSizePx = new Vector2(transform.renderer.bounds.size.y, transform.renderer.bounds.size.x) * ppu;
        wantedRatio = screenHeight/denominator;
      
        Vector2 newSize = new Vector2(1, 1) * wantedRatio;
        transform.localScale = newSize/ppu;
    }
  
}

So my code looks like:

float ScaleIndex = 1f;

float WantedRatio = Screen.height / ScaleByScreenRatio; //(Screen.height / 2f)
ScaleIndex = WantedRatio / 100f;

StaticStorage.ScaleIndex = ScaleIndex;


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…

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?

I already fixed the problem… But since its a ratio (Screen.Height / 2) it should be the same for all density…

How did you fix? Please share, in case someone else has that issue. :wink: