Fixed aspect ratio; window position

This is a twofold question:

1.: I have set a preferred resolution (1024x768; windowed) for my PC standalone build. But I allow the player to change the resolution via an ingame options menu. The problem is, that the window doesn’t stay centered when the resolution is set to 1600 wide (my monitor’s resolution is 1600x1200). It is shifted to the left, partway on my secondary monitor. Is there anything I can do to prevent that?
(addition: can I make it that the preferred resolution is changed, so that the standalone player starts with the new resolution instead of the resolution I set in the player settings? I tried PlayerSettings.default…, but it throws an error when I try to compile: Unknown Identifier: ‘PlayerSettings’. Is it possible that this feature is only editable with C#?)

2.: The player can also change from windowed to full screen. But when the current resolution doesn’t match the aspect ratio of the screen, the game appears stretched. Can I fix that? Maybe add black bars at the top and bottom (or left/right on a widescreen monitor with 4:3 resolution)

1: The size of the screen is defined from one of the corners, so if it’s windowed there’s not really much Unity can do to fix that (considering that window placement is up to your desktop environment of choice).

The actual preferred setting is stored in ‘PlayerPrefs’- you can access it just like any pref you would set otherwise. I’m afraid I don’t know exactly what you need to change in order to modify the screen setup on Windows, but I do know that on MacOS it is stored in a string named ‘NSWindow Frame ScreenSetup’. This makes things kind of difficult- the values are numeric, but stored as a series of numbers separated by spaces.

I would just override the automatic screen res setting when you load the game. If you store the custom resolution as two integers in PlayerPrefs, you should be able to simply disregard the one at startup. This isn’t a good solution, but I can’t really see a better one without more clear information about exactly how the quality dialogue stores its settings.

‘PlayerSettings’ is an editor class, so you can’t compile it without importing the ‘UnityEditor’ namespace. This means you won’t be able to use it in your finished build, either- since it won’t compile. In any case, ‘PlayerSettings’ is the class for modifying the properties of the executable that will be built when you press the ‘Build’ button- not the settings for the player itself. You are probably thinking of ‘PlayerPrefs’.

2: In the player settings (the ones discussed just before), there is a dropdown labelled ‘Supported Aspect Ratios’. Simply drop this down, and only select the aspect ratios that your game should support. This may or may not actually do what you want it to- if it instead simply limits you to screens of a specific aspect ratio (instead of implementing letterboxing etc.), you should come up with some kind of system that detects if it would be a problem, and doesn’t let you pick those resolutions.