- Home /
Screen.SetResolution not behaving as expected
Hey, I've been having an issue that has been making me feel insane for a couple of days now. I've looked everywhere but nothing seems to work.
Whenever I use Screen.SetResolution(), the aspect ratio doesn't change correctly. When I start the game, the highest resolution my monitor supports is applied and looks correct. But, whenever I change my resolution, the aspect ratio (no matter what resolution I choose) gets set to something like 4:3 or 5:4. Obviously, I want the aspect ratio to reflect what the resolution's aspect ratio is. I've attached scripts from my resolution dropdown and my display manager (a script so I can change resolution, fullscreen, etc. from anywhere) and screenshots of my player settings and what the window looks like after I change the resolution.
Resolution Dropdown:
Resolution[] resolutions;
Dropdown resolutionDropdown;
public void Start()
{
resolutionDropdown = GetComponent<Dropdown>();
resolutions = Screen.resolutions;
resolutionDropdown.ClearOptions();
List<string> options = new List<string>();
int currentResolutionIndex = 0;
for (int i = 0; i < resolutions.Length; i++)
{
//I didn't want multiple of the same resolution just with different refresh rates appearing, so I added this check to make sure there aren't duplicates.
if (i > 0 && resolutions[i].width == resolutions[i - 1].width && resolutions[i].height == resolutions[i - 1].height) { }
else
{
string option = resolutions[i].width + " x " + resolutions[i].height;
options.Add(option);
if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height) currentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(options);
resolutionDropdown.value = currentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}
//This is called when a player selects a resolution from the dropdown
public void SetResolution(int resolutionIndex)
{
//Since I change the value in Start (when showing all the options), I need to add this check so it doesn't automatically change.
//If you have another way of doing this that is better, please let me know.
if (Time.timeSinceLevelLoad > 0.1f)
{
Resolution resolution = resolutions[resolutionIndex];
PlayerPrefs.SetInt("resolutionX", resolution.width);
PlayerPrefs.SetInt("resolutionY", resolution.height);
PlayerPrefs.Save();
DisplayManager.instance.AdjustResolution();
}
}
DisplayManger:
public static DisplayManager instance;
void Start()
{
instance = this;
}
public void AdjustResolution()
{
bool fullscreen = false;
if (PlayerPrefs.GetInt("fullscreen") == 1) fullscreen = true;
else fullscreen = false;
Screen.SetResolution(PlayerPrefs.GetInt("resolutionX"), PlayerPrefs.GetInt("resolutionY"), fullscreen);
//This prints out the correct resolution that I just selected.
Debug.Log("Resolution: " + PlayerPrefs.GetInt("resolutionX") + " x " + PlayerPrefs.GetInt("resolutionY") + ". Fullscreen: " + fullscreen);
}
Player settings. I've tried changing the fullscreen mode but that doesn't seem to work either.
Then here is what the game looks like when I am running it. It is supposed to be 1920 x 1080. I should mention that my monitor is 3440 x 1440, but even selecting 3440 x 1440 produces a similar result.
Any ideas about what may be causing this and how I could fix it?
Your answer
Follow this Question
Related Questions
Aspect Ratio ( supporting different screen resolutions ) 1 Answer
size of window 0 Answers
Clamping after forcing aspect ratio 0 Answers