Camera.main.orthographicSize not reporting correct size in start method

Hi All

I’m having a problem with my code where Camera.main.orthographicSize is reporting the size of the camera in the editor of 7.68 regardless of what size I set in the game view, my main camera is set to size 7.68 and viewport Rect set to Width 4 Height 3

 public virtual void Start()
{
      screenHeight = Camera.main.orthographicSize;
      screenWidth = screenHeight * Camera.main.aspect;

     Debug.Log("screenHeight " + screenHeight);
    Debug.Log("screenWidth " + screenWidth);
}

When I run it set at 1920*1080 in the game view, or change the resolution in game view, it still reports the camera height as being 7.68 regardless of the resolution I run it at, but the width is reported as 13.65333 implying the main aspect is being updated correctly to 16:9 from the 4:3 set in the editor.
Screen.width and Screen.height are reported correctly.

Any Help much appreciated

The orthographic size is a value in world space units, and does not change with resolution (pixel units). By setting orthographic size, you are defining how many world space units your vertical resolution represents, and as the docs say, the horizontal orthographic size is determined by the resolution’s aspect ratio.

Hope that helps.

2 Likes

Thanks Jeff I had figured it out, but your reply confirms what I thought, It seems a lot of people like myself have a hard time wrapping our heads around how Unity deals with different screen sizes and I couldn’t find a single explanation which really nails it. basically the height always remains the same as the one set in the unity editor, scaling the sprites accordingly to fit into that height, then for the differing aspect ratio’s of devices, unity adds or subtracts extra horizontal space, the devices actual screen size in pixels basically becomes obsolete.

My only concern now is, is that scaling of the sprites on actual devices good quality ? coming from a background of absolute screen sizes and generating different sprite sheets for different resolutions, its a bit hard for my brain to fully trust it

It’s dependent on your art style. If your sprites contain a lot of high frequency detail, they will lose information if they are downscaled. Or if you want a pixel-perfect look, you’ll need to make sure they scale uniformly to keep pixels square etc.

Generally speaking most art styles will be fine (especially if it’s designed for mobile) so your art wont even need to be scaled that dramatically, because as you said they scale based on aspect ratio, not resolution.

2 Likes

Thanks again Jeff I think I’ll play it by ear, when its ready run on an actual device, I’ll see what it looks like, I’ve already put the ability to toggle the cameras resolution to match the display height on and off, into my custom UI animation framework, so I’ll just leave it as Unity’s default scaling for the time being.

Thanks mate !