Viewport problem when building game on a laptop

Hi!

So, I have started the Laser Defender part of the course. There is this problem when using a different aspect ratio to the one that my computer has:

The camera which I set to 9:16 for example in the unity editor, becomes the 1920*1080 of my screen as soon as I build it into a .exe and try to play the game.

The problems are: My player whom I have clamped to the edges of the 9:16 camera game scene now goes out of that space in the game. (Probably because the ViewPort changes to the wider one as soon as I build the game?)

I temporarily solved this by trial and error increasing the ‘padding’ variable in x axis to bout 13. I have to change it back to 0 or 1 when I start working in the Unity editor again.

Second problem that I just hit: I put the spawn position of my enemy outside the camera range so that they appear from outside. However, in the game that is built, they just appear outside 9:16 area.

I guess, I can put an opaque object like a blank black png on both sides of the scene that covers the sides. But I can’t help but think that I must be doing something fundamentally wrong. Is there a way to build the game with the aspect ratio preserved? Or is it better to just make the game in the computer’s screen aspect all the time?

Thank you in advance for your help. Cheers :slight_smile:

Hi Chironex,

Did you set the correct resolution in File > Build Settings > Player Settings?

I did try changing the aspect ratio in the player Settings. But the standalone build does not use it.

Next I found this solution on the internet:

But as you can see, red squiggly lines say that I can’t put float there. I have to put int. So I just put 16/9 instead. When I attach that to my camera and run:

It’s better I guess. There is some letterboxing. But still not correct aspect ratio. Probably because that int usage rounds down stuff(?).

Here’s another solution on the internet that seems to have worked:

public class ScreenResolution : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        // set the desired aspect ratio (the values in this example are
        // hard-coded for 9:16, but you could make them into public
        // variables instead so you can set them at design time)
        float targetaspect = 9.0f / 16.0f;

        // determine the game window's current aspect ratio
        float windowaspect = (float)Screen.width / (float)Screen.height;

        // current viewport height should be scaled by this amount
        float scaleheight = windowaspect / targetaspect;

        // obtain camera component so we can modify its viewport
        Camera camera = GetComponent<Camera>();

        // if scaled height is less than current height, add letterbox
        if (scaleheight < 1.0f)
        {
            Rect rect = camera.rect;

            rect.width = 1.0f;
            rect.height = scaleheight;
            rect.x = 0;
            rect.y = (1.0f - scaleheight) / 2.0f;

            camera.rect = rect;
        }
        else // add pillarbox
        {
            float scalewidth = 1.0f / scaleheight;

            Rect rect = camera.rect;

            rect.width = scalewidth;
            rect.height = 1.0f;
            rect.x = (1.0f - scalewidth) / 2.0f;
            rect.y = 0;

            camera.rect = rect;
        }
    }

}

This seems to have solved most of the problems. Here’s ho the screen looks in standalone now:

I must confess I don’t completely understand that solution yet. Maybe as I follow along with Rick, it will get more lucid. :slight_smile:

Is there a simpler solution? Something I am doing wrong in the first place.

I found another solution!

In the Unity asset store, there is an asset called ‘AutoLetterBox’. It’s free. If you download it and import it in the project, there’s a new gameobject that can be created called ‘Force Camera Ratios’.

You just enter the required aspect ratio and it works like a charm. Probably easiest for newbies like me :smiley:

You need to enter a suitable height and width in the standalone settings for it to use that. Since we want a 9:16 aspect we would enter something that matches that ratio. I have used a matching size value in my example and as you can there is no extra space etc in the game window and things look/work as they do in the unity editor.

Thank you codermonk. I guess the windowed mode works. I had previously put in values: 1080 and 1920 like the canvas pixels. That had not worked due to some reason. I don’t know why.

Does that mean the problem is solved? :slight_smile:

Yes. Thank You. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms