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

Question Setting camera aspect ratio through rect works in editor, but not in build.

Discussion in 'Scripting' started by PowerJake, May 15, 2022.

  1. PowerJake

    PowerJake

    Joined:
    Nov 15, 2019
    Posts:
    61
    Unity 2021.3.0f1

    Hey, I'm trying to keep my gameplay camera in a 4:3 aspect ratio, regardless of the current display's size. It works in the editor with whatever resolution I have the game window set to. But it always uses the 16:9 ratio of my monitor in the build.

    Here is the code. It is called in the start method of a script attached my camera.
    Code (CSharp):
    1. private void SetCameraAspectRatio() {
    2.         if (m_Camera == null)
    3.             return;
    4.  
    5.         float margin = 0;
    6.         float aspectWidth = 4;
    7.         float aspectHeight = 3;
    8.         m_Camera.aspect = aspectWidth/aspectHeight;
    9.         Display target = Display.displays[m_Camera.targetDisplay];
    10.        
    11.         // update aspect info
    12.         displayWidth = target.renderingWidth;
    13.         displayHeight = target.renderingHeight;
    14.         gameHeight = displayHeight - margin*2;
    15.         gameWidth = aspectWidth * gameHeight / aspectHeight;
    16.        
    17.         Vector2 size = new Vector2(gameWidth/displayWidth, gameHeight/displayHeight);
    18.         Vector2 pos = new Vector2((displayWidth*0.5f - gameWidth*0.5f)/displayWidth, margin/displayHeight);
    19.         Rect newRect = new Rect(pos.x, pos.y, size.x, size.y);
    20.         m_Camera.rect = newRect;
    21.  
    22.         CameraAspectRatioChanged?.Invoke();
    23.     }

    Here it is playing in the editor. Note the black bars on the side appearing as intended.
    upload_2022-5-15_15-56-41.png

    And here it is, not working in the build, exposing the edges of the map.
    upload_2022-5-15_16-0-45.png

    I'm guessing that something about the build is resetting the the aspect ratio of the camera. But that is a total guess. Any input to help me fix this would be greatly appreciated! Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Do a quick print out of the numbers you get in line 12 and 13... years ago there was an issue where dimensions where wrong on the first frame for early-happening messages.

    My solution is always to wait a frame, then do this sort of stuff.
     
  3. PowerJake

    PowerJake

    Joined:
    Nov 15, 2019
    Posts:
    61
    I didn't have any luck making the setup wait for a frame. But I did uncover some other weirdness in my project that may be related. I can't get my project to show the unity console in build. The debug.log with my screen size is nowhere to be found in my player.log file. It is hard to tell what is happening so I'm gonna try to figure that out first. I might make a new topic on that before coming back to this as my searches so far haven't found any help.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I make ALL my Unity games now with a "zeroscene" as the first scene, basically black camera clear with a single script going to the first "real" scene.
     
  5. PowerJake

    PowerJake

    Joined:
    Nov 15, 2019
    Posts:
    61
    Thanks but that didn't fix it either.
     
  6. PowerJake

    PowerJake

    Joined:
    Nov 15, 2019
    Posts:
    61
    I gave up on using unity's built in debug console and added someone's custom script to see debug messages and yeah, the cached screen size was messed up. It worked in the editor because I set some values in OnValidate() and forgot to set them in the build. Thanks for the zeroscene idea by the way. That has cleaned up my game bootstrapping process.
     
    Kurt-Dekker likes this.