Graphic Options changes

Created Diff never expires
62 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
282 lines
84 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
302 lines
using System;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI;


namespace AC
namespace AC
{
{


public class GraphicOptions : MonoBehaviour
public class GraphicOptions : MonoBehaviour
{
{


#region Variables
#region Variables


[SerializeField] private string globalStringVariable = "GraphicOptionsData";
[SerializeField] private string globalStringVariable = "GraphicOptionsData";
[SerializeField] private Dropdown resolutionDropdown = null;
[SerializeField] private Dropdown resolutionDropdown = null;
[SerializeField] private Toggle fullScreenToggle = null;
[SerializeField] private Toggle fullScreenToggle = null;
[SerializeField] private Dropdown qualityPresetDropdown = null;
[SerializeField] private Dropdown qualityPresetDropdown = null;
[SerializeField] private Dropdown antiAliasingDropdown = null;
[SerializeField] private Dropdown antiAliasingDropdown = null;
[SerializeField] private Dropdown textureQualityDropdown = null;
[SerializeField] private Dropdown textureQualityDropdown = null;
[SerializeField] private Dropdown vSyncDropdown = null;
[SerializeField] private Dropdown vSyncDropdown = null;
[SerializeField]
private List<float> supportedAspectRatios = new List<float> {
16/9f,
16/10f,
21/9f
};
[SerializeField] private List<Resolution> supportedResolutions = new List<Resolution>();
private int nonCustomQualityLevel;
private int nonCustomQualityLevel;


#endregion
#endregion




#region UnityStandards
#region UnityStandards


private void OnEnable ()
private void OnEnable()
{
{
UpdateUIValues ();
UpdateUIValues();
}
}


#endregion
#endregion




#region PublicFunctions
#region PublicFunctions


public void SaveAndApply ()
public void SaveAndApply()
{
{
GVar gVar = GlobalVariables.GetVariable (globalStringVariable);
GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
if (gVar != null && gVar.type == VariableType.String)
if (gVar != null && gVar.type == VariableType.String)
{
{
bool usingAdvancedOptions = qualityPresetDropdown.value == qualityPresetDropdown.options.Count - 1;
bool usingAdvancedOptions = qualityPresetDropdown.value == qualityPresetDropdown.options.Count - 1;
GraphicOptionsData graphicOptionsData = new GraphicOptionsData (resolutionDropdown.value, fullScreenToggle.isOn, usingAdvancedOptions ? nonCustomQualityLevel : qualityPresetDropdown.value, usingAdvancedOptions, antiAliasingDropdown.value, textureQualityDropdown.value, vSyncDropdown.value);
GraphicOptionsData graphicOptionsData = new GraphicOptionsData(supportedResolutions, resolutionDropdown.value, fullScreenToggle.isOn, usingAdvancedOptions ? nonCustomQualityLevel : qualityPresetDropdown.value, usingAdvancedOptions, antiAliasingDropdown.value, textureQualityDropdown.value, vSyncDropdown.value);
gVar.TextValue = JsonUtility.ToJson (graphicOptionsData);
gVar.TextValue = JsonUtility.ToJson(graphicOptionsData);
Options.SavePrefs ();
Options.SavePrefs();
}
}
else
else
{
{
ACDebug.LogWarning ("Could not apply Graphic Options data because no Global String variable was found", this);
ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
}
}


Apply ();
Apply();
}
}




public void Apply ()
public void Apply()
{
{
GraphicOptionsData graphicOptionsData = GetSaveData ();
GraphicOptionsData graphicOptionsData = GetSaveData();
if (graphicOptionsData != null)
if (graphicOptionsData != null)
{
{
graphicOptionsData.Apply ();
graphicOptionsData.Apply();
}
}
}
}




public void OnSetAdvancedOption ()
public void OnSetAdvancedOption()
{
{
SetDropdownValue (qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
}
}




public void OnSetQualityPreset ()
public void OnSetQualityPreset()
{
{
if (qualityPresetDropdown.value <= qualityPresetDropdown.options.Count - 1)
if (qualityPresetDropdown.value <= qualityPresetDropdown.options.Count - 1)
{
{
QualitySettings.SetQualityLevel (qualityPresetDropdown.value, false);
QualitySettings.SetQualityLevel(qualityPresetDropdown.value, false);
UpdateAdvancedUIValues ();
UpdateAdvancedUIValues();
QualitySettings.SetQualityLevel (nonCustomQualityLevel, false);
QualitySettings.SetQualityLevel(nonCustomQualityLevel, false);


nonCustomQualityLevel = qualityPresetDropdown.value;
nonCustomQualityLevel = qualityPresetDropdown.value;
}
}
}
}




public static int QualityIndexToLevel (int index)
public static int QualityIndexToLevel(int index)
{
{
return (int) Mathf.Pow (2, index);
return (int)Mathf.Pow(2, index);
}
}




public static int QualityLevelToIndex (int level)
public static int QualityLevelToIndex(int level)
{
{
switch (level)
switch (level)
{
{
case 0:
case 0:
default:
default:
return 0;
return 0;


case 2:
case 2:
return 1;
return 1;


case 4:
case 4:
return 2;
return 2;


case 8:
case 8:
return 3;
return 3;
}
}
}
}


#endregion
#endregion




#region PrivateFunctions
#region PrivateFunctions

private void UpdateUIValues ()
private void UpdateUIValues()
{
{
// Advanced options
// Advanced options
GraphicOptionsData graphicOptionsData = GetSaveData ();
GraphicOptionsData graphicOptionsData = GetSaveData();
bool usingAdvancedOptions = (graphicOptionsData != null) ? graphicOptionsData.UsingAdvancedOptions : false;
bool usingAdvancedOptions = (graphicOptionsData != null) ? graphicOptionsData.UsingAdvancedOptions : false;


// Resolution
// Resolution

//filter for supported aspect ratios
for (int i = 0; i < Screen.resolutions.Length; i++)
{
if (supportedAspectRatios.Contains((float)Screen.resolutions[i].width / (float)Screen.resolutions[i].height))
{
supportedResolutions.Add(Screen.resolutions[i]);
}
}

if (resolutionDropdown)
if (resolutionDropdown)
{
{
resolutionDropdown.options.Clear ();
resolutionDropdown.options.Clear();
int resolutionIndex = -1;
int resolutionIndex = -1;
for (int i = 0; i < Screen.resolutions.Length; i++)
for (int i = 0; i < supportedResolutions.Count; i++)
{
{
if (Screen.resolutions[i].width == Screen.width &&
if (supportedResolutions[i].width == Screen.width &&
Screen.resolutions[i].height == Screen.height &&
supportedResolutions[i].height == Screen.height &&
Screen.resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
supportedResolutions[i].refreshRate == Screen.currentResolution.refreshRate)
{
{
resolutionIndex = i;
resolutionIndex = i;
}
}


string label = Screen.resolutions[i].width.ToString () + " x " + Screen.resolutions[i].height.ToString () + " " + Screen.resolutions[i].refreshRate.ToString () + " hz";
string label = supportedResolutions[i].width.ToString() + " x " + supportedResolutions[i].height.ToString() + " " + supportedResolutions[i].refreshRate.ToString() + " hz";
resolutionDropdown.options.Add (new Dropdown.OptionData (label));
resolutionDropdown.options.Add(new Dropdown.OptionData(label));
}
}
resolutionDropdown.RefreshShownValue ();
resolutionDropdown.RefreshShownValue();
if (resolutionIndex >= 0) SetDropdownValue (resolutionDropdown, resolutionIndex);
if (resolutionIndex >= 0) SetDropdownValue(resolutionDropdown, resolutionIndex);
}
}


// Full-screen
// Full-screen
if (fullScreenToggle)
if (fullScreenToggle)
{
{
fullScreenToggle.isOn = Screen.fullScreen;
fullScreenToggle.isOn = Screen.fullScreen;
}
}


// Quality preset
// Quality preset
if (qualityPresetDropdown)
if (qualityPresetDropdown)
{
{
qualityPresetDropdown.options.Clear ();
qualityPresetDropdown.options.Clear();
foreach (string qualityName in QualitySettings.names)
foreach (string qualityName in QualitySettings.names)
{
{
qualityPresetDropdown.options.Add (new Dropdown.OptionData (qualityName));
qualityPresetDropdown.options.Add(new Dropdown.OptionData(qualityName));
}
}
qualityPresetDropdown.options.Add (new Dropdown.OptionData ("Custom"));
qualityPresetDropdown.options.Add(new Dropdown.OptionData("Custom"));
qualityPresetDropdown.RefreshShownValue ();
qualityPresetDropdown.RefreshShownValue();
if (usingAdvancedOptions)
if (usingAdvancedOptions)
{
{
SetDropdownValue (qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
SetDropdownValue(qualityPresetDropdown, qualityPresetDropdown.options.Count - 1);
}
}
else
else
{
{
SetDropdownValue (qualityPresetDropdown, QualitySettings.GetQualityLevel ());
SetDropdownValue(qualityPresetDropdown, QualitySettings.GetQualityLevel());
}
}
nonCustomQualityLevel = QualitySettings.GetQualityLevel ();
nonCustomQualityLevel = QualitySettings.GetQualityLevel();
}
}


UpdateAdvancedUIValues ();
UpdateAdvancedUIValues();
}
}


private void UpdateAdvancedUIValues ()

private void UpdateAdvancedUIValues()
{
{
// Anti-aliasing
// Anti-aliasing
int antiAliasingValue = QualityLevelToIndex (QualitySettings.antiAliasing);
int antiAliasingValue = QualityLevelToIndex(QualitySettings.antiAliasing);
SetDropdownValue (antiAliasingDropdown, antiAliasingValue);
SetDropdownValue(antiAliasingDropdown, antiAliasingValue);


// Texture quality
// Texture quality
SetDropdownValue (textureQualityDropdown, QualitySettings.masterTextureLimit);
SetDropdownValue(textureQualityDropdown, QualitySettings.masterTextureLimit);


// Vsync
// Vsync
SetDropdownValue (vSyncDropdown, QualitySettings.vSyncCount);
SetDropdownValue(vSyncDropdown, QualitySettings.vSyncCount);
}
}




private GraphicOptionsData GetSaveData ()
private GraphicOptionsData GetSaveData()
{
{
GVar gVar = GlobalVariables.GetVariable (globalStringVariable);
GVar gVar = GlobalVariables.GetVariable(globalStringVariable);
if (gVar != null && gVar.type == VariableType.String)
if (gVar != null && gVar.type == VariableType.String)
{
{
string optionsDataString = gVar.TextValue;
string optionsDataString = gVar.TextValue;
if (!string.IsNullOrEmpty (optionsDataString))
if (!string.IsNullOrEmpty(optionsDataString))
{
{
GraphicOptionsData graphicOptionsData = JsonUtility.FromJson<GraphicOptionsData> (optionsDataString);
GraphicOptionsData graphicOptionsData = JsonUtility.FromJson<GraphicOptionsData>(optionsDataString);
return graphicOptionsData;
return graphicOptionsData;
}
}
return null;
return null;
}
}
else
else
{
{
ACDebug.LogWarning ("Could not apply Graphic Options data because no Global String variable was found", this);
ACDebug.LogWarning("Could not apply Graphic Options data because no Global String variable was found", this);
return null;
return null;
}
}
}
}




private void SetDropdownValue (Dropdown dropdown, int value)
private void SetDropdownValue(Dropdown dropdown, int value)
{
{
if (dropdown)
if (dropdown)
{
{
dropdown.SetValueWithoutNotify (value);
dropdown.SetValueWithoutNotify(value);
}
}
}
}


#endregion
#endregion


}
}




[Serializable]
[Serializable]
public class GraphicOptionsData
public class GraphicOptionsData
{
{


#region Variables
#region Variables


[SerializeField] private List<Resolution> supportedResolutions = new List<Resolution>();
[SerializeField] private int screenResolutionIndex;
[SerializeField] private int screenResolutionIndex;
[SerializeField] private bool isFullScreen;
[SerializeField] private bool isFullScreen;
[SerializeField] private int qualityPresetIndex;
[SerializeField] private int qualityPresetIndex;
[SerializeField] private bool usingAdvancedOptions;
[SerializeField] private bool usingAdvancedOptions;
[SerializeField] private int antiAliasingLevel;
[SerializeField] private int antiAliasingLevel;
[SerializeField] private int textureQualityLevel;
[SerializeField] private int textureQualityLevel;
[SerializeField] private int vSyncCount;
[SerializeField] private int vSyncCount;


#endregion
#endregion




#region Constructors
#region Constructors


public GraphicOptionsData (int _screenResolutionIndex, bool _isFullScreen, int _qualityPresetIndex, bool _usingAdvancedOptions, int _antiAliasingLevel, int _textureQualityLevel, int _vSyncCount)
public GraphicOptionsData(List<Resolution> _supportedResolutions, int _screenResolutionIndex, bool _isFullScreen, int _qualityPresetIndex, bool _usingAdvancedOptions, int _antiAliasingLevel, int _textureQualityLevel, int _vSyncCount)
{
{
supportedResolutions = _supportedResolutions;
screenResolutionIndex = _screenResolutionIndex;
screenResolutionIndex = _screenResolutionIndex;
isFullScreen = _isFullScreen;
isFullScreen = _isFullScreen;
qualityPresetIndex = _qualityPresetIndex;
qualityPresetIndex = _qualityPresetIndex;
usingAdvancedOptions = _usingAdvancedOptions;
usingAdvancedOptions = _usingAdvancedOptions;
antiAliasingLevel = _antiAliasingLevel;
antiAliasingLevel = _antiAliasingLevel;
textureQualityLevel = _textureQualityLevel;
textureQualityLevel = _textureQualityLevel;
vSyncCount = _vSyncCount;
vSyncCount = _vSyncCount;
}
}


#endregion
#endregion




#region PublicFunctions
#region PublicFunctions


public void Apply ()
public void Apply()
{
{
Resolution chosenResolution = Screen.resolutions[screenResolutionIndex];
Resolution chosenResolution = supportedResolutions[screenResolutionIndex];
Screen.SetResolution (chosenResolution.width, chosenResolution.height, isFullScreen);
Screen.SetResolution(chosenResolution.width, chosenResolution.height, isFullScreen);
QualitySettings.SetQualityLevel (qualityPresetIndex, true);
QualitySettings.SetQualityLevel(qualityPresetIndex, true);
if (usingAdvancedOptions)
if (usingAdvancedOptions)
{
{
QualitySettings.antiAliasing = GraphicOptions.QualityIndexToLevel (antiAliasingLevel);
QualitySettings.antiAliasing = GraphicOptions.QualityIndexToLevel(antiAliasingLevel);
QualitySettings.masterTextureLimit = textureQualityLevel;
QualitySettings.masterTextureLimit = textureQualityLevel;
QualitySettings.vSyncCount = vSyncCount;
QualitySettings.vSyncCount = vSyncCount;
}
}


KickStarter.playerMenus.RecalculateAll ();
KickStarter.playerMenus.RecalculateAll();
}
}


#endregion
#endregion




#region GetSet
#region GetSet


public bool UsingAdvancedOptions { get { return usingAdvancedOptions; } }
public bool UsingAdvancedOptions { get { return usingAdvancedOptions; } }


#endregion
#endregion


}
}


}
}