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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Screen size and camera problem not match

Discussion in 'Editor & General Support' started by theya1112, Jan 15, 2023.

  1. theya1112

    theya1112

    Joined:
    Apr 26, 2021
    Posts:
    2
    My game need to fix camera fit to game and I will create sprite to fit screen same.
    I settings display screen to 640x360 is 16:9
    upload_2023-1-16_4-24-4.png

    and I create sprite 640x320 size (Setting Pixel Per Unit : 32 ) import to my unity
    and then I come to screen found some problem my sprite bigger more than screen or camera,

    upload_2023-1-16_4-39-18.png

    If I count 1 grid is 32 pixel like (Setting Pixel Per Unit : 32 ) my sprite is 20 grid result 20*32 = 640 pixel is right
    but camera is look like around 17.8 *32 = 569 pixel,look like small than my sprite. I don't understand why problem here.

    I try get sprite size, screen size it same size. and camera size

    Code (CSharp):
    1.    SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
    2.         Debug.Log($"sprite size # {spriteRenderer.sprite.rect.width}, {spriteRenderer.sprite.rect.height}");
    3.      
    4.         Camera cam = Camera.main;
    5.         float height = 2f * cam.orthographicSize;
    6.         float width = height * cam.aspect;
    7.         Debug.Log($"Camera # {width}, {height}");
    8.  
    9.  
    10.         Debug.Log("Screen Size : " + Screen.width + "," + Screen.height);
    upload_2023-1-16_4-33-17.png
    this finally detail, I so confuse about this. Please help me to understand something, i try read document but not clear to my problem. I want to set screen and camera to correct size what I want and create sprite for fit to it from art program.

    Thank you.
    Ps.Sorry for my english, I can't typing not strong too much
     

    Attached Files:

  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    The screen size you select in the scene view only affects the screen size (duh), how that translates into what you're camera is seeing and how many world units are visible is dependent on the camera settings and entirely unrelated to screen size.

    You need to adjust your camera's orthographic size to fit your sprite. If your sprite is 11.25 units high (360 / 32), your orthographic size needs to be half of that, so 5.625. From the console output it looks like it's currently set to 5 and therefore cuts off part of the sprite.

    Note that it's often not possible to dictate screen/window size (e.g. on mobile), so it's usually better to dynamically adjust your camera to render only to a part of the screen (i.e. letterboxing) to make sure only a set viewport is visible at all times.
     
  3. theya1112

    theya1112

    Joined:
    Apr 26, 2021
    Posts:
    2
    thank you for reply.Now I understand how it work.