Hello!
I don’t know why this is so hard, but I’m trying to make an options menu that allows the player to change the resolution of the game based off on the available screen resolutions the player’s screen allows.
What I want is for a drop down list to add in those values, then the player can select the values to change them.
void Start() {
resolutionDD.ClearOptions();
resolutions = Screen.resolutions;
for (int i = 0; i < resolutions.Length; i++) {
resolutionDD.options_.text = ResToString(resolutions*);*_
resolutionDD.value = i;
}
}
string ResToString(Resolution res) {
return res.width + “x” + res.height;
}
The code I have currently just returns a blank list and I get this error:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.UI.Dropdown+OptionData].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
SettingsManager.Start () (at Assets/Scripts/GameControl/Settings/SettingsManager.cs:24)
this worked for me. i found the using the ClearOptions didn’t work.
also added to update the text.
dropdownMenu.options.Add(new Dropdown.OptionData(dropdownMenu.options_.text));_
here is the code. copy / paste and enjoy.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScreenResolutions : MonoBehaviour
{
Resolution[] resolutions;
public Dropdown dropdownMenu;
void Start()
{
resolutions = Screen.resolutions;
dropdownMenu.onValueChanged.AddListener(delegate { Screen.SetResolution(resolutions[dropdownMenu.value].width, resolutions[dropdownMenu.value].height, false); });
for (int i = 0; i < resolutions.Length; i++)
{
dropdownMenu.options_.text = ResToString(resolutions*);
dropdownMenu.value = i;_
_dropdownMenu.options.Add(new Dropdown.OptionData(dropdownMenu.options.text));*_
}
}
string ResToString(Resolution res)
{
return res.width + " x " + res.height;
}
}
Ich ergänze mal um:
if (i < resolutions.Length-1)
dropdownMenu.options.Add(new
Damit sollte die letzte Auflösung nicht zweimal auftauchen. Es wird übrigens ein (wenn auch leeres) Element bereits in der Option-Liste des Dropdown erwartet.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScreenResolutions : MonoBehaviour
{
Resolution[] resolutions;
public Dropdown dropdownMenu;
string ResToString(Resolution res)
{
return res.width + " x " + res.height;
}
void Start()
{
resolutions = Screen.resolutions;
dropdownMenu.onValueChanged.AddListener(delegate { Screen.SetResolution(resolutions[dropdownMenu.value].width, resolutions[dropdownMenu.value].height, false); });
for (int i = 0; i < resolutions.Length; i++)
{
dropdownMenu.options_.text = ResToString(resolutions*);*_
dropdownMenu.value = i;
if (i < resolutions.Length-1)
dropdownMenu.options.Add(new Dropdown.OptionData(dropdownMenu.options*.text));*
}
}
}