您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
373 行
11 KiB
373 行
11 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
[System.Serializable]
|
|
public class ShadowDistanceTier
|
|
{
|
|
public float Distance;
|
|
public string TierDescription;
|
|
}
|
|
|
|
public class SettingsGraphicsComponent : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<ShadowDistanceTier> ShadowDistanceTierList = new List<ShadowDistanceTier>(); // filled from inspector
|
|
|
|
[SerializeField] private UniversalRenderPipelineAsset URPAsset = default;
|
|
|
|
private int _savedResolutionIndex = default;
|
|
private int _savedAntiAliasingIndex = default;
|
|
private int _savedShadowDistanceTier = default;
|
|
private bool _savedFullscreenState = default;
|
|
|
|
private int _currentResolutionIndex = default;
|
|
private List<Resolution> _resolutionsList = default;
|
|
[SerializeField] UISettingItemFiller _resolutionsField = default;
|
|
|
|
/* private int _currentShadowQualityIndex = default;
|
|
private List<string> _shadowQualityList = default;
|
|
[SerializeField] private UISettingItemFiller _shadowQualityField = default;*/
|
|
|
|
private int _currentAntiAliasingIndex = default;
|
|
private List<string> _currentAntiAliasingList = default;
|
|
[SerializeField] private UISettingItemFiller _antiAliasingField = default;
|
|
|
|
private int _currentShadowDistanceTier = default;
|
|
[SerializeField] private UISettingItemFiller _shadowDistanceField = default;
|
|
private bool _isFullscreen = default;
|
|
|
|
[SerializeField] private UISettingItemFiller _fullscreenField = default;
|
|
|
|
public event UnityAction<int, int, float, bool> _save = delegate { };
|
|
|
|
private Resolution _currentResolution;
|
|
|
|
[SerializeField] private UIGenericButton _saveButton;
|
|
[SerializeField] private UIGenericButton _resetButton;
|
|
|
|
void OnEnable()
|
|
{
|
|
_resolutionsField._nextOption += NextResolution;
|
|
_resolutionsField._previousOption += PreviousResolution;
|
|
|
|
_shadowDistanceField._nextOption += NextShadowDistanceTier;
|
|
_shadowDistanceField._previousOption += PreviousShadowDistanceTier;
|
|
|
|
_fullscreenField._nextOption += NextFullscreenState;
|
|
_fullscreenField._previousOption += PreviousFullscreenState;
|
|
|
|
_antiAliasingField._nextOption += NextAntiAliasingTier;
|
|
_antiAliasingField._previousOption += PreviousAntiAliasingTier;
|
|
|
|
_saveButton.Clicked += SaveSettings;
|
|
_resetButton.Clicked += ResetSettings;
|
|
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
ResetSettings();
|
|
_resolutionsField._nextOption -= NextResolution;
|
|
_resolutionsField._previousOption -= PreviousResolution;
|
|
|
|
_shadowDistanceField._nextOption -= NextShadowDistanceTier;
|
|
_shadowDistanceField._previousOption -= PreviousShadowDistanceTier;
|
|
|
|
_fullscreenField._nextOption -= NextFullscreenState;
|
|
_fullscreenField._previousOption -= PreviousFullscreenState;
|
|
|
|
_antiAliasingField._nextOption -= NextAntiAliasingTier;
|
|
_antiAliasingField._previousOption -= PreviousAntiAliasingTier;
|
|
|
|
_saveButton.Clicked -= SaveSettings;
|
|
_resetButton.Clicked -= ResetSettings;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Init()
|
|
{
|
|
_resolutionsList = GetResolutionsList();
|
|
_currentShadowDistanceTier = GetCurrentShadowDistanceTier();
|
|
_currentAntiAliasingList = GetDropdownData(Enum.GetNames(typeof(MsaaQuality)));
|
|
|
|
_currentResolution = Screen.currentResolution;
|
|
_currentResolutionIndex = GetCurrentResolutionIndex();
|
|
_isFullscreen = GetCurrentFullscreenState();
|
|
_currentAntiAliasingIndex = GetCurrentAntialiasing();
|
|
|
|
_savedResolutionIndex = _currentResolutionIndex;
|
|
_savedAntiAliasingIndex = _currentAntiAliasingIndex;
|
|
_savedShadowDistanceTier = _currentShadowDistanceTier;
|
|
_savedFullscreenState = _isFullscreen;
|
|
}
|
|
public void Setup()
|
|
{
|
|
Init();
|
|
SetResolutionField();
|
|
SetShadowDistance();
|
|
SetFullscreen();
|
|
SetAntiAliasingField();
|
|
}
|
|
|
|
#region Resolution
|
|
void SetResolutionField()
|
|
{
|
|
string displayText = _resolutionsList[_currentResolutionIndex].ToString();
|
|
displayText = displayText.Substring(0, displayText.IndexOf("@"));
|
|
|
|
_resolutionsField.FillSettingField(_resolutionsList.Count, _currentResolutionIndex, displayText);
|
|
|
|
}
|
|
List<Resolution> GetResolutionsList()
|
|
{
|
|
List<Resolution> options = new List<Resolution>();
|
|
for (int i = 0; i < Screen.resolutions.Length; i++)
|
|
{
|
|
options.Add(Screen.resolutions[i]);
|
|
}
|
|
|
|
return options;
|
|
}
|
|
int GetCurrentResolutionIndex()
|
|
{
|
|
if (_resolutionsList == null)
|
|
{ _resolutionsList = GetResolutionsList(); }
|
|
int index = _resolutionsList.FindIndex(o => o.width == _currentResolution.width && o.height == _currentResolution.height);
|
|
return index;
|
|
}
|
|
void NextResolution()
|
|
{
|
|
_currentResolutionIndex++;
|
|
_currentResolutionIndex = Mathf.Clamp(_currentResolutionIndex, 0, _resolutionsList.Count - 1);
|
|
OnResolutionChange();
|
|
}
|
|
void PreviousResolution()
|
|
{
|
|
_currentResolutionIndex--;
|
|
_currentResolutionIndex = Mathf.Clamp(_currentResolutionIndex, 0, _resolutionsList.Count - 1);
|
|
OnResolutionChange();
|
|
}
|
|
void OnResolutionChange()
|
|
{
|
|
_currentResolution = _resolutionsList[_currentResolutionIndex];
|
|
Screen.SetResolution(_currentResolution.width, _currentResolution.height, _isFullscreen);
|
|
SetResolutionField();
|
|
}
|
|
#endregion
|
|
|
|
#region ShadowDistance
|
|
void SetShadowDistance()
|
|
{
|
|
_shadowDistanceField.FillSettingField_Localized(ShadowDistanceTierList.Count, _currentShadowDistanceTier, ShadowDistanceTierList[_currentShadowDistanceTier].TierDescription);
|
|
}
|
|
int GetCurrentShadowDistanceTier()
|
|
{
|
|
int tierIndex = -1;
|
|
if (ShadowDistanceTierList.Exists(o => o.Distance == URPAsset.shadowDistance))
|
|
tierIndex = ShadowDistanceTierList.FindIndex(o => o.Distance == URPAsset.shadowDistance);
|
|
else
|
|
{
|
|
Debug.LogError("Current shadow distance is not in the tier List " + URPAsset.shadowDistance);
|
|
}
|
|
return tierIndex;
|
|
|
|
}
|
|
void NextShadowDistanceTier()
|
|
{
|
|
_currentShadowDistanceTier++;
|
|
_currentShadowDistanceTier = Mathf.Clamp(_currentShadowDistanceTier, 0, ShadowDistanceTierList.Count);
|
|
OnShadowDistanceChange();
|
|
}
|
|
void PreviousShadowDistanceTier()
|
|
{
|
|
_currentShadowDistanceTier--;
|
|
_currentShadowDistanceTier = Mathf.Clamp(_currentShadowDistanceTier, 0, ShadowDistanceTierList.Count);
|
|
OnShadowDistanceChange();
|
|
}
|
|
|
|
void OnShadowDistanceChange()
|
|
{
|
|
URPAsset.shadowDistance = ShadowDistanceTierList[_currentShadowDistanceTier].Distance;
|
|
SetShadowDistance();
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region fullscreen
|
|
void SetFullscreen()
|
|
{
|
|
if (_isFullscreen)
|
|
{
|
|
_fullscreenField.FillSettingField_Localized(2, 1, "On");
|
|
}
|
|
else
|
|
{
|
|
_fullscreenField.FillSettingField_Localized(2, 0, "Off");
|
|
}
|
|
|
|
}
|
|
bool GetCurrentFullscreenState()
|
|
{
|
|
return Screen.fullScreen;
|
|
}
|
|
void NextFullscreenState()
|
|
{
|
|
_isFullscreen = true;
|
|
OnFullscreenChange();
|
|
}
|
|
void PreviousFullscreenState()
|
|
{
|
|
_isFullscreen = false;
|
|
OnFullscreenChange();
|
|
}
|
|
void OnFullscreenChange()
|
|
{
|
|
Screen.fullScreen = _isFullscreen;
|
|
SetFullscreen();
|
|
}
|
|
#endregion
|
|
|
|
#region Anti Aliasing
|
|
void SetAntiAliasingField()
|
|
{
|
|
string optionDisplay = _currentAntiAliasingList[_currentAntiAliasingIndex].Replace("_", "");
|
|
_antiAliasingField.FillSettingField(_currentAntiAliasingList.Count, _currentAntiAliasingIndex, optionDisplay);
|
|
|
|
}
|
|
int GetCurrentAntialiasing()
|
|
{
|
|
return URPAsset.msaaSampleCount;
|
|
|
|
}
|
|
void NextAntiAliasingTier()
|
|
{
|
|
_currentAntiAliasingIndex++;
|
|
_currentAntiAliasingIndex = Mathf.Clamp(_currentAntiAliasingIndex, 0, _currentAntiAliasingList.Count - 1);
|
|
OnAntiAliasingChange();
|
|
}
|
|
void PreviousAntiAliasingTier()
|
|
{
|
|
_currentAntiAliasingIndex--;
|
|
_currentAntiAliasingIndex = Mathf.Clamp(_currentAntiAliasingIndex, 0, _currentAntiAliasingList.Count - 1);
|
|
OnAntiAliasingChange();
|
|
}
|
|
|
|
void OnAntiAliasingChange()
|
|
{
|
|
URPAsset.msaaSampleCount = _currentAntiAliasingIndex;
|
|
SetAntiAliasingField();
|
|
|
|
}
|
|
#endregion
|
|
|
|
private List<string> GetDropdownData(string[] optionNames, params string[] customOptions)
|
|
{
|
|
List<string> options = new List<string>();
|
|
foreach (string option in optionNames)
|
|
{
|
|
options.Add(option);
|
|
}
|
|
|
|
foreach (string option in customOptions)
|
|
{
|
|
options.Add(option);
|
|
}
|
|
return options;
|
|
}
|
|
|
|
public void SaveSettings()
|
|
{
|
|
|
|
_savedResolutionIndex = _currentResolutionIndex;
|
|
_savedAntiAliasingIndex = _currentAntiAliasingIndex;
|
|
_savedShadowDistanceTier = _currentShadowDistanceTier;
|
|
_savedFullscreenState = _isFullscreen;
|
|
float shadowDistance = ShadowDistanceTierList[_currentShadowDistanceTier].Distance;
|
|
_save.Invoke(_currentResolutionIndex, _currentAntiAliasingIndex, shadowDistance, _isFullscreen);
|
|
}
|
|
public void ResetSettings()
|
|
{
|
|
_currentResolutionIndex = _savedResolutionIndex;
|
|
OnResolutionChange();
|
|
_currentAntiAliasingIndex = _savedAntiAliasingIndex;
|
|
OnAntiAliasingChange();
|
|
_currentShadowDistanceTier = _savedShadowDistanceTier;
|
|
OnShadowDistanceChange();
|
|
_isFullscreen = _savedFullscreenState;
|
|
OnFullscreenChange();
|
|
}
|
|
|
|
|
|
/*
|
|
#region ShadowQuality
|
|
void SetShadowQualityField()
|
|
{
|
|
_currentShadowQualityIndex = GetCurrentShadowQualityIndex();
|
|
_shadowQualityList = GetDropdownData(Enum.GetNames(typeof(UnityEngine.Rendering.Universal.ShadowQuality)));
|
|
_shadowQualityField.FillSettingField_Localized(_shadowQualityList.Count, _currentShadowQualityIndex, _shadowQualityList[_currentShadowQualityIndex]);
|
|
|
|
}
|
|
int GetCurrentShadowQualityIndex()
|
|
{
|
|
UnityEngine.Rendering.Universal.ShadowQuality shadowQuality = UnityEngine.Rendering.Universal.ShadowQuality.Disabled;
|
|
if (URPAsset.supportsMainLightShadows && URPAsset.supportsSoftShadows)
|
|
{
|
|
shadowQuality = UnityEngine.Rendering.Universal.ShadowQuality.SoftShadows;
|
|
|
|
}
|
|
else if (URPAsset.supportsMainLightShadows)
|
|
{
|
|
shadowQuality = UnityEngine.Rendering.Universal.ShadowQuality.HardShadows;
|
|
}
|
|
else
|
|
{
|
|
shadowQuality = UnityEngine.Rendering.Universal.ShadowQuality.Disabled;
|
|
}
|
|
return (int)shadowQuality;
|
|
}
|
|
void SetNextShadowQuality()
|
|
{
|
|
_currentShadowQualityIndex++;
|
|
_currentShadowQualityIndex = Mathf.Clamp(_currentShadowQualityIndex, 0, _shadowQualityList.Count - 1);
|
|
OnShadowQualityChange();
|
|
}
|
|
void SetPreviousShadowQuality()
|
|
{
|
|
_currentShadowQualityIndex--;
|
|
_currentShadowQualityIndex = Mathf.Clamp(_currentShadowQualityIndex, 0, _shadowQualityList.Count - 1);
|
|
OnShadowQualityChange();
|
|
}
|
|
void OnShadowQualityChange()
|
|
{
|
|
UnityEngine.Rendering.Universal.ShadowQuality shadowQuality = (UnityEngine.Rendering.Universal.ShadowQuality)_currentShadowQualityIndex;
|
|
|
|
switch (shadowQuality)
|
|
{
|
|
|
|
case UnityEngine.Rendering.Universal.ShadowQuality.SoftShadows:
|
|
URPAsset.shadow = true;
|
|
URPAsset.supportsSoftShadows = true;
|
|
break;
|
|
|
|
case UnityEngine.Rendering.Universal.ShadowQuality.HardShadows:
|
|
URPAsset.supportsMainLightShadows = true;
|
|
URPAsset.supportsSoftShadows = false;
|
|
break;
|
|
case UnityEngine.Rendering.Universal.ShadowQuality.Disabled:
|
|
URPAsset.supportsMainLightShadows = false;
|
|
URPAsset.supportsSoftShadows = false;
|
|
break;
|
|
}
|
|
SetShadowQualityField();
|
|
}
|
|
|
|
#endregion
|
|
*/
|
|
|
|
|
|
}
|