(EDIT: Not ideal, forcing a lower resolution sometimes. Better solution below.)
Bump,
so I have tried a couple of approaches.
First I tried to play around with the values in the Canvas Scaler script, manually setting screen size that affects the scale factor. The idea was that I would “trick” the Canvas set to Screen Space - Overlay into thinking that the screen size in for example 800x600 is actually 640x360 so the overlay would correctly fit the game camera. Seem tho that the screen size that affects the scale factor in SetScaleFactor(scaleFactor);
doesn’t determine the position, only size, since it just made the UI smaller. If someone knows how to force my intended effect please let me know.
Secondly I know that the Screen Space - Overlay works correctly if the screen size is a multiple of 640x360, I thought that restricting the game to work only in those resolutions would fix my problem. So I made a
ForceResolution script that forces the game to run in “viable” resolutions.
The idea is that I get the highest resolution the monitor supports and set my game resolution to that, then i check the current screen resolution and set the “viable” resolution (e.g. a 800x600 screen fits a 640x360 but 1366x768 can fit a 1280x720 resolution).
This approach “works” but…
Now you might want to sit down for this.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ForceResolution : MonoBehaviour
{
public TextMeshProUGUI res;
public TextMeshProUGUI setGreen;
void Start()
{
Resolution[] resArray = Screen.resolutions;
Screen.SetResolution(resArray[resArray.Length-1].width, resArray[resArray.Length-1].height,true);
res.text = Screen.currentResolution.width.ToString() + ", " + Screen.currentResolution.height.ToString();
if (Display.main.systemWidth >= 640 && Display.main.systemWidth < 1280)
{
Screen.SetResolution(640, 360, true);
setGreen.text = "640, 360";
}
else if (Display.main.systemWidth >= 1280 && Display.main.systemWidth < 1920)
{
Screen.SetResolution(1280, 720, true);
setGreen.text = "1280, 720";
}
else if (Display.main.systemWidth >= 1920 && Display.main.systemWidth < 2560)
{
Screen.SetResolution(1920, 1080, true);
setGreen.text = "1920, 1080";
}
else if (Display.main.systemWidth >= 2560 && Display.main.systemWidth < 3840)
{
Screen.SetResolution(2560, 1440, true);
setGreen.text = "2560, 1440";
}
else if (Display.main.systemWidth >= 3840)
{
Screen.SetResolution(3840, 2160, true);
setGreen.text = "3840, 2160";
}
}
private void Update()
{
res.text = Screen.currentResolution.width.ToString() + ", " + Screen.currentResolution.height.ToString();
}
}
Now I know that this isn’t viable or possibly even legal but I have been sitting on this issue for over 2 weeks.
This takes care of the issue with UI being outside of game view and it is not shaking since its on the Screen Space - Overlay but the thought that this all could be fixed if I somehow could got rid of the shaking on the Screen Space - Camera really keeps me up at night.
If anyone can help me out with this issue, please, do let me know.