Advertisement
Guest User

Untitled

a guest
Oct 11th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. namespace AC
  7. {
  8.  
  9.     public class GraphicOptions : MonoBehaviour
  10.     {
  11.  
  12.         #region Variables
  13.  
  14.         [SerializeField] private string globalStringVariable = "GraphicOptionsData";
  15.         [SerializeField] private Dropdown resolutionDropdown = null;
  16.         [SerializeField] private Toggle fullScreenToggle = null;
  17.         [SerializeField] private Dropdown qualityPresetDropdown = null;
  18.         [SerializeField] private Dropdown antiAliasingDropdown = null;
  19.         [SerializeField] private Dropdown textureQualityDropdown = null;
  20.         [SerializeField] private Dropdown vSyncDropdown = null;
  21.         [SerializeField]
  22.         private List<float> supportedAspectRatios = new List<float> {
  23.             16/9f,
  24.             16/10f,
  25.             21/9f
  26.         };
  27.         [SerializeField] private List<Resolution> supportedResolutions = new List<Resolution>();
  28.         private int nonCustomQualityLevel;
  29.  
  30.         #endregion
  31.  
  32.  
  33.         #region UnityStandards
  34.  
  35.         private void OnEnable()
  36.         {
  37.             UpdateUIValues();
  38.         }
  39.  
  40.         #endregion
  41.  
  42.  
  43.         #region PublicFunctions
  44.  
  45.         public void SaveAndApply()
  46.         {
  47.             GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
  48.             if (gVar != null && gVar.type == VariableType.String)
  49.             {
  50.                 bool usingAdvancedOptions = qualityPresetDropdown.value == qualityPresetDropdown.options.Count - 1;
  51.                 GraphicOptionsData graphicOptionsData = new GraphicOptionsData(supportedResolutions, resolutionDropdown.value, fullScreenToggle.isOn, usingAdvancedOptions ? nonCustomQualityLevel : qualityPresetDropdown.value, usingAdvancedOptions, antiAliasingDropdown.value, textureQualityDropdown.value, vSyncDropdown.value);
  52.                 gVar.TextValue = JsonUtility.ToJson(graphicOptionsData);
  53.                 Options.SavePrefs();
  54.             }
  55.             else
  56.             {
  57.                 ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
  58.             }
  59.  
  60.             Apply();
  61.         }
  62.  
  63.  
  64.         public void Apply()
  65.         {
  66.             GraphicOptionsData graphicOptionsData = GetSaveData();
  67.             if (graphicOptionsData != null)
  68.             {
  69.                 graphicOptionsData.Apply();
  70.             }
  71.         }
  72.  
  73.  
  74.         public void OnSetAdvancedOption()
  75.         {
  76.             SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
  77.         }
  78.  
  79.  
  80.         public void OnSetQualityPreset()
  81.         {
  82.             if (qualityPresetDropdown.value <= qualityPresetDropdown.options.Count - 1)
  83.             {
  84.                 QualitySettings.SetQualityLevel(qualityPresetDropdown.value, false);
  85.                 UpdateAdvancedUIValues();
  86.                 QualitySettings.SetQualityLevel(nonCustomQualityLevel, false);
  87.  
  88.                 nonCustomQualityLevel = qualityPresetDropdown.value;
  89.             }
  90.         }
  91.  
  92.  
  93.         public static int QualityIndexToLevel(int index)
  94.         {
  95.             return (int)Mathf.Pow(2, index);
  96.         }
  97.  
  98.  
  99.         public static int QualityLevelToIndex(int level)
  100.         {
  101.             switch (level)
  102.             {
  103.                 case 0:
  104.                 default:
  105.                     return 0;
  106.  
  107.                 case 2:
  108.                     return 1;
  109.  
  110.                 case 4:
  111.                     return 2;
  112.  
  113.                 case 8:
  114.                     return 3;
  115.             }
  116.         }
  117.  
  118.         #endregion
  119.  
  120.  
  121.         #region PrivateFunctions
  122.        
  123.         private void UpdateUIValues()
  124.         {
  125.             // Advanced options
  126.             GraphicOptionsData graphicOptionsData = GetSaveData();
  127.             bool usingAdvancedOptions = (graphicOptionsData != null) ? graphicOptionsData.UsingAdvancedOptions : false;
  128.  
  129.             // Resolution
  130.  
  131.             //filter for supported aspect ratios
  132.             for (int i = 0; i < Screen.resolutions.Length; i++)
  133.             {
  134.                 if (supportedAspectRatios.Contains((float)Screen.resolutions[i].width / (float)Screen.resolutions[i].height))
  135.                 {
  136.                     supportedResolutions.Add(Screen.resolutions[i]);
  137.                 }
  138.             }
  139.  
  140.             if (resolutionDropdown)
  141.             {
  142.                 resolutionDropdown.options.Clear();
  143.                 int resolutionIndex = -1;
  144.                 for (int i = 0; i < supportedResolutions.Count; i++)
  145.                 {
  146.                     if (supportedResolutions[i].width == Screen.width &&
  147.                         supportedResolutions[i].height == Screen.height &&
  148.                         supportedResolutions[i].refreshRate == Screen.currentResolution.refreshRate)
  149.                     {
  150.                         resolutionIndex = i;
  151.                     }
  152.  
  153.                     string label = supportedResolutions[i].width.ToString() + " x " + supportedResolutions[i].height.ToString() + " " + supportedResolutions[i].refreshRate.ToString() + " hz";
  154.                     resolutionDropdown.options.Add(new Dropdown.OptionData(label));
  155.                 }
  156.                 resolutionDropdown.RefreshShownValue();
  157.                 if (resolutionIndex >= 0) SetDropdownValue(resolutionDropdown, resolutionIndex);
  158.             }
  159.  
  160.             // Full-screen
  161.             if (fullScreenToggle)
  162.             {
  163.                 fullScreenToggle.isOn = Screen.fullScreen;
  164.             }
  165.  
  166.             // Quality preset
  167.             if (qualityPresetDropdown)
  168.             {
  169.                 qualityPresetDropdown.options.Clear();
  170.                 foreach (string qualityName in QualitySettings.names)
  171.                 {
  172.                     qualityPresetDropdown.options.Add(new Dropdown.OptionData(qualityName));
  173.                 }
  174.                 qualityPresetDropdown.options.Add(new Dropdown.OptionData("Custom"));
  175.                 qualityPresetDropdown.RefreshShownValue();
  176.                 if (usingAdvancedOptions)
  177.                 {
  178.                     SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
  179.                 }
  180.                 else
  181.                 {
  182.                     SetDropdownValue(qualityPresetDropdown, QualitySettings.GetQualityLevel());
  183.                 }
  184.                 nonCustomQualityLevel = QualitySettings.GetQualityLevel();
  185.             }
  186.  
  187.             UpdateAdvancedUIValues();
  188.         }
  189.  
  190.  
  191.         private void UpdateAdvancedUIValues()
  192.         {
  193.             // Anti-aliasing
  194.             int antiAliasingValue = QualityLevelToIndex(QualitySettings.antiAliasing);
  195.             SetDropdownValue(antiAliasingDropdown, antiAliasingValue);
  196.  
  197.             // Texture quality
  198.             SetDropdownValue(textureQualityDropdown, QualitySettings.masterTextureLimit);
  199.  
  200.             // Vsync
  201.             SetDropdownValue(vSyncDropdown, QualitySettings.vSyncCount);
  202.         }
  203.  
  204.  
  205.         private GraphicOptionsData GetSaveData()
  206.         {
  207.             GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
  208.             if (gVar != null && gVar.type == VariableType.String)
  209.             {
  210.                 string optionsDataString = gVar.TextValue;
  211.                 if (!string.IsNullOrEmpty(optionsDataString))
  212.                 {
  213.                     GraphicOptionsData graphicOptionsData = JsonUtility.FromJson<GraphicOptionsData>(optionsDataString);
  214.                     return graphicOptionsData;
  215.                 }
  216.                 return null;
  217.             }
  218.             else
  219.             {
  220.                 ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
  221.                 return null;
  222.             }
  223.         }
  224.  
  225.  
  226.         private void SetDropdownValue(Dropdown dropdown, int value)
  227.         {
  228.             if (dropdown)
  229.             {
  230.                 dropdown.SetValueWithoutNotify(value);
  231.             }
  232.         }
  233.  
  234.         #endregion
  235.  
  236.     }
  237.  
  238.  
  239.     [Serializable]
  240.     public class GraphicOptionsData
  241.     {
  242.  
  243.         #region Variables
  244.  
  245.         [SerializeField] private List<Resolution> supportedResolutions = new List<Resolution>();
  246.         [SerializeField] private int screenResolutionIndex;
  247.         [SerializeField] private bool isFullScreen;
  248.         [SerializeField] private int qualityPresetIndex;
  249.         [SerializeField] private bool usingAdvancedOptions;
  250.         [SerializeField] private int antiAliasingLevel;
  251.         [SerializeField] private int textureQualityLevel;
  252.         [SerializeField] private int vSyncCount;
  253.  
  254.         #endregion
  255.  
  256.  
  257.         #region Constructors
  258.  
  259.         public GraphicOptionsData(List<Resolution> _supportedResolutions, int _screenResolutionIndex, bool _isFullScreen, int _qualityPresetIndex, bool _usingAdvancedOptions, int _antiAliasingLevel, int _textureQualityLevel, int _vSyncCount)
  260.         {
  261.             supportedResolutions = _supportedResolutions;
  262.             screenResolutionIndex = _screenResolutionIndex;
  263.             isFullScreen = _isFullScreen;
  264.             qualityPresetIndex = _qualityPresetIndex;
  265.             usingAdvancedOptions = _usingAdvancedOptions;
  266.             antiAliasingLevel = _antiAliasingLevel;
  267.             textureQualityLevel = _textureQualityLevel;
  268.             vSyncCount = _vSyncCount;
  269.         }
  270.  
  271.         #endregion
  272.  
  273.  
  274.         #region PublicFunctions
  275.  
  276.         public void Apply()
  277.         {
  278.             Resolution chosenResolution = supportedResolutions[screenResolutionIndex];
  279.             Screen.SetResolution(chosenResolution.width, chosenResolution.height, isFullScreen);
  280.             QualitySettings.SetQualityLevel(qualityPresetIndex, true);
  281.             if (usingAdvancedOptions)
  282.             {
  283.                 QualitySettings.antiAliasing = GraphicOptions.QualityIndexToLevel(antiAliasingLevel);
  284.                 QualitySettings.masterTextureLimit = textureQualityLevel;
  285.                 QualitySettings.vSyncCount = vSyncCount;
  286.             }
  287.  
  288.             KickStarter.playerMenus.RecalculateAll();
  289.         }
  290.  
  291.         #endregion
  292.  
  293.  
  294.         #region GetSet
  295.  
  296.         public bool UsingAdvancedOptions { get { return usingAdvancedOptions; } }
  297.  
  298.         #endregion
  299.  
  300.     }
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement