HKI Core
UIResolutionDropdown.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections.Generic;
4 using HKI.Core.Variables;
5 using TMPro;
6 
7 namespace HKI.Core.UI.Settings
8 {
12  [RequireComponent(typeof(TMP_Dropdown))]
13  public class UIResolutionDropdown : MonoBehaviour
14  {
15  // Public variables
16  [SerializeField] IntegerWidthHeight Resolution = null;
17 
18  // Private variables
19  TMP_Dropdown dropdown = null;
20 
21  List<IntWidthHeight> resolutions = new List<IntWidthHeight>();
22 
23  // Awake function
24  void Awake()
25  {
26  dropdown = GetComponent<TMP_Dropdown>();
27  }
28 
29  // Start function
30  void Start()
31  {
32  if(Resolution != null)
33  {
34  Resolution[] res = Screen.resolutions;
35  for (int i = 0; i < res.Length; i++)
36  {
37  if(resolutions.FindIndex(x => x.Width == res[i].width && x.Height == res[i].height) < 0)
38  resolutions.Add(new IntWidthHeight() { Width = res[i].width, Height = res[i].height });
39  }
40 
41  List<TMP_Dropdown.OptionData> list = new List<TMP_Dropdown.OptionData>();
42  int currentResolutionIndex = 0;
43  Resolution currentResolution = Screen.currentResolution;
44  for(int i = 0; i < resolutions.Count; i++)
45  {
46  list.Add(new TMP_Dropdown.OptionData(resolutions[i].Width.ToString() + " x " + resolutions[i].Height.ToString()));
47 
48  if (resolutions[i].Width == currentResolution.width && resolutions[i].Height == currentResolution.height)
49  currentResolutionIndex = i;
50  }
51 
52  dropdown.ClearOptions();
53  dropdown.AddOptions(list);
54 
55  dropdown.value = currentResolutionIndex;
56  }
57  else
58  Debug.LogError("(UIResolutionDropdown) Resolution value is null!");
59  }
60 
61  // Interface functions
63  {
64  if(Resolution != null)
65  Resolution.SetValue = resolutions[dropdown.value];
66  }
67 
68  public void Reset()
69  {
70  Resolution[] res = Screen.resolutions;
71  for (int i = 0; i < res.Length; i++)
72  {
73  if (resolutions.FindIndex(x => x.Width == res[i].width && x.Height == res[i].height) < 0)
74  resolutions.Add(new IntWidthHeight() { Width = res[i].width, Height = res[i].height });
75  }
76 
77  List<TMP_Dropdown.OptionData> list = new List<TMP_Dropdown.OptionData>();
78  int currentResolutionIndex = 0;
79  Resolution currentResolution = Screen.currentResolution;
80  for (int i = 0; i < resolutions.Count; i++)
81  {
82  list.Add(new TMP_Dropdown.OptionData(resolutions[i].Width.ToString() + " x " + resolutions[i].Height.ToString()));
83 
84  if (resolutions[i].Width == currentResolution.width && resolutions[i].Height == currentResolution.height)
85  currentResolutionIndex = i;
86  }
87 
88  dropdown.ClearOptions();
89  dropdown.AddOptions(list);
90 
91  dropdown.value = currentResolutionIndex;
92  }
93  }
94 }
This is a data type that combines a int width and a int height value into one value. This can be used for storing resolution information.
This MonoBehaviour fills and handles a dropdown (TMP_Dropdown) with resolution settings.
Implementation of a IntegerWidthHeight value as a HKIVar via HKIVarGeneric.