I try to filter Screen.resolutions according to aspect ratio but it create empty array.
Here’s my code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
public class SettingsMenu : MonoBehaviour
{
public AudioMixer audioMixer;
public TMP_Dropdown resolutionDropdown,aspectRatioDropdown;
Resolution[] resolutions, resolutions16_9, resolutions16_10, resolutions4_3;
string[] ratios;
float currentRatio;
float ratio;
int CurrentResolutionIndex = 0;
int CurrentRatioIndex = 0;
List<string> resolutionsOptions = new List<string>();
List<string> aspectRatios = new List<string>();
private void Start()
{
// this work fine
resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == Screen.currentResolution.refreshRate).ToArray();
//adding ratios to array and list
// this work fine
aspectRatioDropdown.ClearOptions();
ratios = new string[] { "4:3", "16:10", "16:9" };
currentRatio = (float)Screen.width / (float)Screen.height;
aspectRatios.Add("4:3");
aspectRatios.Add("16:10");
aspectRatios.Add("16:9");
if (currentRatio == 4f / 3f)
{
CurrentRatioIndex = 0;
}
else if (currentRatio == 16f / 10f)
{
CurrentRatioIndex = 1;
}
else if (currentRatio == 16f / 9f)
{
CurrentRatioIndex = 2;
}
aspectRatioDropdown.AddOptions(aspectRatios);
aspectRatioDropdown.value = CurrentRatioIndex;
aspectRatioDropdown.RefreshShownValue();
Debug.Log(CurrentRatioIndex);
Debug.Log(Screen.width);
Debug.Log(Screen.height);
Debug.Log(currentRatio);
// there is a problem
if (CurrentRatioIndex == 0)
{
//resolutions4_3 = resolutions.Where(resolution => (float)resolution.width / (float)resolution.height == 4f / 3f).ToArray();
resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == Screen.currentResolution.refreshRate && (float)resolution.width / (float)resolution.height == 4f / 3f).ToArray();
Debug.Log("4:3");
}
else if (CurrentRatioIndex == 1)
{
//resolutions16_10 = resolutions.Where(resolution => (float)resolution.width / (float)resolution.height == 16f / 10f).ToArray();
resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == Screen.currentResolution.refreshRate && (float)resolution.width / (float)resolution.height == 16f / 10f).ToArray();
Debug.Log("16:10 ");
}
else if (CurrentRatioIndex == 2)
{
//resolutions16_9 = resolutions.Where(resolution => (float)resolution.width / (float)resolution.height == 16f / 9f).ToArray();
resolutions = Screen.resolutions.Where(resolution => resolution.refreshRate == Screen.currentResolution.refreshRate && (float)resolution.width / (float)resolution.height == 16f / 9f).ToArray();
Debug.Log("16:9 ");
}
Debug.Log(resolutions.Length);
//Debug.Log("length 4:3: " + resolutions4_3.Length);
//Debug.Log("length 16:10: " + resolutions16_10.Length);
//Debug.Log("lenght 16:9: " + resolutions16_9.Length);
// this work fine
resolutionDropdown.ClearOptions();
resolutionsOptions.Clear();
for (int i = 0; i < resolutions.Length; i++)
{
string resolutionOption = resolutions[i].width + " x " + resolutions[i].height;
resolutionsOptions.Add(resolutionOption);
if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height)
{
CurrentResolutionIndex = i;
}
}
resolutionDropdown.AddOptions(resolutionsOptions);
resolutionDropdown.value = CurrentResolutionIndex;
resolutionDropdown.RefreshShownValue();
}