Although we’ve gone over the build process several times already, I would have liked it if we did it one more time for this game as well. I say this because the issue of how to handle an unusual aspect ratio like 9:16 on a PC build has not really come up before in this course. On the other hand, I was able to figure it out, so perhaps it should have just been mentioned as something to be aware of when finishing up this game.
The Q&A for this lesson has a few solutions to this issue, but they all depend on the user being limited to a pre-defined window rather than full-screen. I didn’t like that approach, so I found an alternative that I think is more robust than those discussed on the Q&A because it allows full-screen.
First, I created a script based on the code that is described here: https://forum.unity.com/threads/aspect-ratio-issue-on-build.543722/ and attached it to the MainCamera prefab. All this script does is limit the width of the viewport to the 9:16 ratio, using a calculation based on whatever screen dimensions the game starts up in. This had some other side effects, though: the canvas was still the width of the screen, ignoring the viewport limit, and the player’s ship could be moved outside the viewport limits. So I changed the setting in all of the canvas objects to Render Mode: Screen Space - Camera, and linked it to the Main Camera. I also set the canvas to Match Height instead of Width (under Canvas Scaler).
The player was still moving off-screen though, even though the code Rick walked us through should limit the movement to the viewport. What I was missing was that the start method in the aspect ratio correction script was being processed by Unity after the start method in the player script that defined the movement limits and padding. I changed the build settings to make sure the aspect ratio script ran first, although I suspect moving the aspect ration fix to “awake()” instead of “start()” would have worked as well. Finally, I changed the “player settings” in the Build menu so that the background is black so that the pillarboxes around the viewport blend with the game better.
Anyway, now I have a PC build that will work properly in full screen mode, or any window size the player selects, without changing the aspect ratio of the playing field or the UI.