resolution.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. public class resolution : MonoBehaviour {
  7. int index = 0;
  8. private Resolution[] resolutionList;
  9. /// <summary>
  10. /// 获取分辨率信息
  11. /// </summary>
  12. public Resolution Get_resolution
  13. {
  14. get { return resolutionList[index]; }
  15. }
  16. [SerializeField]
  17. private Dropdown resoluDropdown;
  18. [SerializeField]
  19. private Toggle FWtg;//获取切换全屏的toggle控件
  20. public bool FullWindow { get; set; }//是否全屏的状态
  21. //[SerializeField]
  22. //private Text rosoluDisplay; //分辨率文字显示
  23. private void Start()
  24. {
  25. //清除分辨率下拉框的内容
  26. //resoluDropdown.ClearOptions();
  27. //初始化全屏开关
  28. FullWindow = Screen.fullScreen;
  29. FWtg.isOn = FullWindow;
  30. //规定分辨率数组长度
  31. resolutionList = new Resolution[Screen.resolutions.Length-6];
  32. //Debug.Log(Screen.currentResolution);
  33. //挑选有用的值给分辨率列表
  34. List<string> DropdownList = new List<string>();
  35. for (int i = 0; i < resolutionList.Length; i++)
  36. {
  37. resolutionList[i] = Screen.resolutions[i+6];//添加相应的分辨率到列表
  38. DropdownList.Add(resolutionList[i].width + " × " + resolutionList[i].height);
  39. // Debug.Log(resolutionList[i]);
  40. }
  41. // Debug.Log(resolutionList.Length);
  42. resoluDropdown.AddOptions(DropdownList);
  43. int _width = Screen.currentResolution.width;
  44. int _height = Screen.currentResolution.height;
  45. //设定当前分辨率的序号
  46. for (int i = 0; i < resolutionList.Length; i++)
  47. {
  48. if (resolutionList[i].width == _width && resolutionList[i].height == _height)
  49. {
  50. index = i;
  51. continue;
  52. }
  53. }
  54. //rosoluDisplay.text = _width + " × " + _height;
  55. resoluDropdown.value = index;
  56. //resolutionText.text = _width + " × " + _height;
  57. }
  58. public void FullWindowToggle(Toggle tg) {
  59. FullWindow = tg.isOn;
  60. //Debug.Log(FullWindow);
  61. }
  62. public void indexChange()
  63. {
  64. index = resoluDropdown.value;
  65. // index = index + sum >= resolutionList.Length ? 0 : index + sum < 0 ? resolutionList.Length - 1 : index + sum;
  66. var _width = resolutionList[index].width;
  67. var _height = resolutionList[index].height;
  68. //rosoluDisplay.text = _width + " × " + _height;
  69. Screen.SetResolution(_width, _height, FullWindow);
  70. // Debug.Log(index);
  71. }
  72. }