Skip to content

Instantly share code, notes, and snippets.

@QubitsDev
Created August 26, 2017 01:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QubitsDev/12227a7164f1ae99c1a7537c5c98068c to your computer and use it in GitHub Desktop.
Save QubitsDev/12227a7164f1ae99c1a7537c5c98068c to your computer and use it in GitHub Desktop.
Automatic quality level dropdown.
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Dropdown))]
public class QualityDropdown : MonoBehaviour
{
private Dropdown m_Dropdown;
/// <summary>
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
/// </summary>
void Start()
{
m_Dropdown = GetComponent<Dropdown>();
m_Dropdown.options.Clear();
for (int i = 0; i < QualitySettings.names.Length; i++)
{
m_Dropdown.options.Add(new Dropdown.OptionData(QualitySettings.names[i]));
m_Dropdown.onValueChanged.AddListener(QualitySettings.SetQualityLevel);
}
// Set current quality level.
m_Dropdown.value = QualitySettings.GetQualityLevel();
}
}
@Renardjojo
Copy link

Renardjojo commented Nov 3, 2022

You need add listener once only.
You can also reduce line count with this:

[RequireComponent(typeof(TMP_Dropdown))]
public class SettingController : MonoBehaviour
{
    private TMP_Dropdown m_Dropdown;
    
    void Start()
    {
        m_Dropdown = GetComponent<TMP_Dropdown>();
        
        m_Dropdown.AddOptions(QualitySettings.names.ToList());
        m_Dropdown.onValueChanged.AddListener(QualitySettings.SetQualityLevel);
        m_Dropdown.value = QualitySettings.GetQualityLevel();
    }
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment