Files
BlueArchiveMiniGame/Assets/Scripts/HotUpdate/Main/UI/SettingWindow/GraphicsPanel.cs

178 lines
5.9 KiB
C#
Raw Normal View History

2025-09-17 18:56:28 +08:00
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using TMPro;
using System.Collections;
2025-09-29 07:45:07 +08:00
public class GraphicsPanel : UIBasePanel, ISettingsPanel
2025-09-17 18:56:28 +08:00
{
[Header("UI References")]
[SerializeField] private TMP_Dropdown qualityDropdown;
[SerializeField] private Toggle vsyncToggle;
[SerializeField] private Slider shadowDistanceSlider;
[SerializeField] private TMP_Dropdown antiAliasingDropdown;
[SerializeField] private Toggle bloomToggle;
[SerializeField] private Slider renderScaleSlider;
[Header("Graphics References")]
[SerializeField] private VolumeProfile urpVolumeProfile;
[Header("Quality Presets")]
[SerializeField]
private QualityPreset[] qualityPresets = new QualityPreset[4]
{
new QualityPreset("Low", 0, false, 20f, 0, false, 0.8f),
new QualityPreset("Medium", 1, false, 40f, 1, true, 1.0f),
new QualityPreset("High", 2, true, 60f, 2, true, 1.2f),
new QualityPreset("Ultra", 3, true, 100f, 2, true, 1.5f)
};
[System.Serializable]
public class QualityPreset
{
public string name;
public int qualityLevel;
public bool vsyncEnabled;
public float shadowDistance;
public int antiAliasing;
public bool bloomEnabled;
public float renderScale;
public QualityPreset(string name, int qualityLevel, bool vsyncEnabled, float shadowDistance,
int antiAliasing, bool bloomEnabled, float renderScale)
{
this.name = name;
this.qualityLevel = qualityLevel;
this.vsyncEnabled = vsyncEnabled;
this.shadowDistance = shadowDistance;
this.antiAliasing = antiAliasing;
this.bloomEnabled = bloomEnabled;
this.renderScale = renderScale;
}
}
private UniversalRenderPipelineAsset _urpAsset;
private Bloom _bloom;
private void OnEnable()
{
StartCoroutine(InitializeDelayed());
}
private IEnumerator InitializeDelayed()
{
2025-09-29 07:45:07 +08:00
yield return null;
2025-09-17 18:56:28 +08:00
_urpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
if (_urpAsset == null)
{
2025-09-29 07:45:07 +08:00
Debug.LogError("URP Asset null");
2025-09-17 18:56:28 +08:00
yield break;
}
if (urpVolumeProfile != null && !urpVolumeProfile.TryGet(out _bloom))
{
2025-09-29 07:45:07 +08:00
Debug.LogWarning("Volume Profile Bloom null");
2025-09-17 18:56:28 +08:00
}
//InitializeQualityDropdown();
2025-09-29 07:45:07 +08:00
var settings = SettingsManager.Inst.CurrentSettings;
2025-09-17 18:56:28 +08:00
qualityDropdown.SetValueWithoutNotify(settings.qualityLevel);
vsyncToggle.SetIsOnWithoutNotify(settings.vsyncEnabled);
shadowDistanceSlider.SetValueWithoutNotify(settings.shadowDistance);
antiAliasingDropdown.SetValueWithoutNotify(settings.antiAliasing);
bloomToggle.SetIsOnWithoutNotify(settings.bloomEnabled);
renderScaleSlider.SetValueWithoutNotify(settings.renderScale);
ApplySettings();
}
//private void InitializeQualityDropdown()
//{
// qualityDropdown.ClearOptions();
// foreach (var preset in qualityPresets)
// {
// qualityDropdown.options.Add(new TMP_Dropdown.OptionData(preset.name));
// }
//}
public void OnQualityChanged(int index)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.qualityLevel = index;
var settings = SettingsManager.Inst.CurrentSettings;
2025-09-17 18:56:28 +08:00
if (index >= 0 && index < qualityPresets.Length)
{
var preset = qualityPresets[index];
2025-09-29 07:45:07 +08:00
// Ӧ<><D3A6>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-09-17 18:56:28 +08:00
vsyncToggle.SetIsOnWithoutNotify(preset.vsyncEnabled);
shadowDistanceSlider.SetValueWithoutNotify(preset.shadowDistance);
antiAliasingDropdown.SetValueWithoutNotify(preset.antiAliasing);
bloomToggle.SetIsOnWithoutNotify(preset.bloomEnabled);
renderScaleSlider.SetValueWithoutNotify(preset.renderScale);
}
2025-09-29 07:45:07 +08:00
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-09-17 18:56:28 +08:00
OnVSyncChanged(settings.vsyncEnabled);
OnShadowDistanceChanged(settings.shadowDistance);
OnAntiAliasingChanged(settings.antiAliasing);
OnBloomChanged(settings.bloomEnabled);
OnRenderScaleChanged(settings.renderScale);
}
public void OnVSyncChanged(bool value)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.vsyncEnabled = value;
2025-09-17 18:56:28 +08:00
QualitySettings.vSyncCount = value ? 1 : 0;
}
public void OnShadowDistanceChanged(float value)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.shadowDistance = value;
2025-09-17 18:56:28 +08:00
if (_urpAsset != null)
_urpAsset.shadowDistance = value;
}
public void OnAntiAliasingChanged(int index)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.antiAliasing = index;
2025-09-17 18:56:28 +08:00
if (_urpAsset != null)
_urpAsset.msaaSampleCount = (int)Mathf.Pow(2, index);
}
public void OnBloomChanged(bool value)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.bloomEnabled = value;
2025-09-17 18:56:28 +08:00
if (_bloom != null)
_bloom.active = value;
}
public void OnRenderScaleChanged(float value)
{
2025-09-29 07:45:07 +08:00
SettingsManager.Inst.CurrentSettings.renderScale = value;
2025-09-17 18:56:28 +08:00
if (_urpAsset != null)
_urpAsset.renderScale = value;
}
public void ApplySettings()
{
if (_urpAsset == null) return;
2025-09-29 07:45:07 +08:00
var settings = SettingsManager.Inst.CurrentSettings;
2025-09-17 18:56:28 +08:00
if (settings.qualityLevel != 4)
QualitySettings.SetQualityLevel(settings.qualityLevel);
QualitySettings.vSyncCount = settings.vsyncEnabled ? 1 : 0;
_urpAsset.shadowDistance = settings.shadowDistance;
_urpAsset.msaaSampleCount = (int)Mathf.Pow(2, settings.antiAliasing);
_urpAsset.renderScale = settings.renderScale;
if (_bloom != null) _bloom.active = settings.bloomEnabled;
}
2025-09-29 07:45:07 +08:00
public void ResetToDefault()
{
throw new System.NotImplementedException();
}
2025-09-17 18:56:28 +08:00
}