UI框架开发中

This commit is contained in:
2025-09-29 11:03:26 +08:00
parent 71edbb6088
commit 3ea4257e2d
24 changed files with 292 additions and 266 deletions

View File

@@ -8,4 +8,14 @@ public abstract class UIBasePanel : MonoBehaviour
public virtual void OnHide() { }
public virtual void Refresh() { }
public virtual void Show()
{
gameObject.SetActive(true);
OnShow();
}
public virtual void Hide()
{
gameObject.SetActive(false);
OnHide();
}
}

View File

@@ -11,7 +11,7 @@ public abstract class UIBaseWindow : MonoBehaviour
}
public virtual void Hide()
{
gameObject.SetActive(true);
gameObject.SetActive(false);
OnHide();
}
protected virtual void OnShow() { }

View File

@@ -6,22 +6,22 @@ public class AudioPanel : UIBasePanel, ISettingsPanel
{
[SerializeField] private Slider masterVolumeSlider;
[SerializeField] private AudioMixer audioMixer;
private void OnEnable()
float masterVolume;
public override void OnShow()
{
masterVolumeSlider.value = SettingsManager.Inst.CurrentSettings.masterVolume;
masterVolume = masterVolumeSlider.value = SettingsManager.Inst.CurrentSettings.masterVolume;
if (audioMixer == null) audioMixer = FindAnyObjectByType<AudioMixer>();
}
public void OnMasterVolumeChanged(float value)
{
SettingsManager.Inst.CurrentSettings.masterVolume = value;
masterVolume = value;
SetVolume(value);
}
public void ApplySettings()
{
SetVolume(SettingsManager.Inst.CurrentSettings.masterVolume);
SettingsManager.Inst.CurrentSettings.masterVolume = masterVolume;
}
private void SetVolume(float volume)
@@ -32,6 +32,6 @@ public class AudioPanel : UIBasePanel, ISettingsPanel
public void ResetToDefault()
{
throw new System.NotImplementedException();
SettingsManager.Inst.CurrentSettings.masterVolume = masterVolume = SettingsManager.Inst.DefSetting.masterVolume;
}
}

View File

@@ -5,29 +5,22 @@ using UnityEngine.UI;
public class GeneralPanel : UIBasePanel,ISettingsPanel
{
[SerializeField] private TMP_InputField serverAddressInput;
string serverAddress;
public override void OnShow()
{
serverAddressInput.text = SettingsManager.Inst.CurrentSettings.serverAddress;
base.Refresh();
}
public override void Refresh()
{
base.Refresh();
}
public void OnServerAddressChanged(string value)
{
SettingsManager.Inst.CurrentSettings.serverAddress = value;
serverAddress = value;
}
public void ApplySettings()
{
// SettingsManager.Inst.CurrentSettings.serverAddress = value;
SettingsManager.Inst.CurrentSettings.serverAddress = serverAddress;
}
public void ResetToDefault()
{
SettingsManager.Inst.CurrentSettings.serverAddress = serverAddress = SettingsManager.Inst.DefSetting.serverAddress;
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using YooAsset;
public class GraphicsManager : Singleton<GraphicsManager>
{
public GraphicsSetting graphicsSetting;
private UniversalRenderPipelineAsset _urpAsset;
private VolumeProfile urpVolumeProfile;
public GraphicsManager()
{
_urpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
if (_urpAsset == null)
{
Debug.LogError("URP Asset null");
}
urpVolumeProfile = YooAssets.LoadAssetSync("SampleSceneProfile").AssetObject as VolumeProfile;
if (urpVolumeProfile == null)
{
Debug.LogError("VolumeProfile null");
}
graphicsSetting = SettingsManager.Inst.CurrentSettings.graphicsSettings;
ApplySettings();
}
public void ApplySettings()
{
SettingsManager.Inst.CurrentSettings.graphicsSettings = graphicsSetting;
QualitySettings.vSyncCount = graphicsSetting.vsyncEnabled ? 1 : 0;
_urpAsset.shadowDistance = graphicsSetting.shadowDistance;
_urpAsset.msaaSampleCount = (int)Mathf.Pow(2, graphicsSetting.qualityLevel);
_urpAsset.renderScale = graphicsSetting.renderScale;
}
}
[System.Serializable]
public class GraphicsSetting
{
public int qualityLevel = 2;
public bool vsyncEnabled = true;
public float shadowDistance = 50f;
public int antiAliasing = 2;
public bool bloomEnabled = true;
public float renderScale = 1.0f;
public bool isCustom = false;
public GraphicsSetting() { }
public GraphicsSetting(int quality, bool vsync, float shadows, int aa, bool bloom, float scale)
{
qualityLevel = quality;
vsyncEnabled = vsync;
shadowDistance = shadows;
antiAliasing = aa;
bloomEnabled = bloom;
renderScale = scale;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 557d237147ace7e4bb16964db0441340
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -15,160 +15,51 @@ public class GraphicsPanel : UIBasePanel, ISettingsPanel
[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]
public override void OnShow()
{
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()
{
yield return null;
_urpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
if (_urpAsset == null)
{
Debug.LogError("URP Asset null");
yield break;
}
if (urpVolumeProfile != null && !urpVolumeProfile.TryGet(out _bloom))
{
Debug.LogWarning("Volume Profile Bloom null");
}
//InitializeQualityDropdown();
var settings = SettingsManager.Inst.CurrentSettings;
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();
qualityDropdown.SetValueWithoutNotify(settings.graphicsSettings.qualityLevel);
vsyncToggle.SetIsOnWithoutNotify(settings.graphicsSettings.vsyncEnabled);
shadowDistanceSlider.SetValueWithoutNotify(settings.graphicsSettings.shadowDistance);
antiAliasingDropdown.SetValueWithoutNotify(settings.graphicsSettings.antiAliasing);
bloomToggle.SetIsOnWithoutNotify(settings.graphicsSettings.bloomEnabled);
renderScaleSlider.SetValueWithoutNotify(settings.graphicsSettings.renderScale);
}
//private void InitializeQualityDropdown()
//{
// qualityDropdown.ClearOptions();
// foreach (var preset in qualityPresets)
// {
// qualityDropdown.options.Add(new TMP_Dropdown.OptionData(preset.name));
// }
//}
public void OnQualityChanged(int index)
{
SettingsManager.Inst.CurrentSettings.qualityLevel = index;
var settings = SettingsManager.Inst.CurrentSettings;
if (index >= 0 && index < qualityPresets.Length)
{
var preset = qualityPresets[index];
// Ӧ<><D3A6>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
vsyncToggle.SetIsOnWithoutNotify(preset.vsyncEnabled);
shadowDistanceSlider.SetValueWithoutNotify(preset.shadowDistance);
antiAliasingDropdown.SetValueWithoutNotify(preset.antiAliasing);
bloomToggle.SetIsOnWithoutNotify(preset.bloomEnabled);
renderScaleSlider.SetValueWithoutNotify(preset.renderScale);
}
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
OnVSyncChanged(settings.vsyncEnabled);
OnShadowDistanceChanged(settings.shadowDistance);
OnAntiAliasingChanged(settings.antiAliasing);
OnBloomChanged(settings.bloomEnabled);
OnRenderScaleChanged(settings.renderScale);
GameSystem.Inst.Graphics.graphicsSetting.qualityLevel = index;
}
public void OnVSyncChanged(bool value)
{
SettingsManager.Inst.CurrentSettings.vsyncEnabled = value;
QualitySettings.vSyncCount = value ? 1 : 0;
GameSystem.Inst.Graphics.graphicsSetting.vsyncEnabled = value;
}
public void OnShadowDistanceChanged(float value)
{
SettingsManager.Inst.CurrentSettings.shadowDistance = value;
if (_urpAsset != null)
_urpAsset.shadowDistance = value;
GameSystem.Inst.Graphics.graphicsSetting.shadowDistance = value;
}
public void OnAntiAliasingChanged(int index)
{
SettingsManager.Inst.CurrentSettings.antiAliasing = index;
if (_urpAsset != null)
_urpAsset.msaaSampleCount = (int)Mathf.Pow(2, index);
GameSystem.Inst.Graphics.graphicsSetting.antiAliasing = index;
}
public void OnBloomChanged(bool value)
{
SettingsManager.Inst.CurrentSettings.bloomEnabled = value;
if (_bloom != null)
_bloom.active = value;
GameSystem.Inst.Graphics.graphicsSetting.bloomEnabled = value;
}
public void OnRenderScaleChanged(float value)
{
SettingsManager.Inst.CurrentSettings.renderScale = value;
if (_urpAsset != null)
_urpAsset.renderScale = value;
GameSystem.Inst.Graphics.graphicsSetting.renderScale = value;
}
public void ApplySettings()
{
if (_urpAsset == null) return;
var settings = SettingsManager.Inst.CurrentSettings;
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;
GameSystem.Inst.Graphics.ApplySettings();
}
public void ResetToDefault()

View File

@@ -7,6 +7,7 @@ public class SettingsManager: Singleton<SettingsManager>
{
private GameSettings _currentSettings;
public GameSettings CurrentSettings => _currentSettings;
public GameSettings DefSetting = new GameSettings();
private string settingsPath;
public SettingsManager()
{
@@ -33,9 +34,14 @@ public class SettingsManager: Singleton<SettingsManager>
string json = JsonUtility.ToJson(_currentSettings, true);
File.WriteAllText(settingsPath, json);
}
#region
#endregion
#region
#endregion
public void ResetToDefaultSettings()
{
_currentSettings = new GameSettings();
_currentSettings = DefSetting;
SaveSettings();
}
}
@@ -43,16 +49,17 @@ public class SettingsManager: Singleton<SettingsManager>
[System.Serializable]
public class GameSettings
{
public string serverAddress = "http://127.0.0.1:8080";
public string serverAddress ;
public float masterVolume = 0.75f;
public int qualityLevel = 2;
public bool vsyncEnabled = true;
public float shadowDistance = 50f;
public int antiAliasing = 2;
public bool bloomEnabled = true;
public float renderScale = 1.0f;
//public int qualityLevel = 2;
//public bool vsyncEnabled = true;
//public float shadowDistance = 50f;
//public int antiAliasing = 2;
//public bool bloomEnabled = true;
//public float renderScale = 1.0f;
public GraphicsSetting graphicsSettings;
public int resolutionIndex = 0;
public bool fullscreen = true;

View File

@@ -34,7 +34,7 @@ public class SettingsWindow : UIBaseWindow
displayTab.onClick.AddListener(() => SwitchPanel(displayPanel));
applyButton.onClick.AddListener(ApplyAllSettings);
cancelButton.onClick.AddListener(() => { UIManager.Inst.HideWindow(gameObject.name); });
cancelButton.onClick.AddListener(() => { UIManager.Inst.HideWindow(nameof(SettingsWindow)); });
defaultsButton.onClick.AddListener(ResetToDefaults);
quitButton.onClick.AddListener(() => { Application.Quit(); });
}
@@ -72,7 +72,7 @@ public class SettingsWindow : UIBaseWindow
SettingsManager.Inst.SaveSettings();
UIManager.Inst.HideWindow(gameObject.name);
UIManager.Inst.HideWindow(nameof(SettingsWindow));
}
public void ResetToDefaults()

View File

@@ -4,7 +4,7 @@ using System.Collections.Generic;
using UnityEngine;
using YooAsset;
public class UIManager : SingletonMono<UIManager>
public class UIManager : Singleton<UIManager>
{
Transform uiRoot;
Dictionary<string, UIBaseWindow> openedWindows = new Dictionary<string, UIBaseWindow>();
@@ -20,9 +20,10 @@ public class UIManager : SingletonMono<UIManager>
{
YooAssets.LoadAssetAsync<GameObject>(windowName).Completed += handle =>
{
GameObject go = Instantiate((GameObject)handle.AssetObject, GameManager.Inst.MainUICanvas.transform);
GameObject go = GameObject.Instantiate((GameObject)handle.AssetObject, GameManager.Inst.MainUICanvas.transform);
var window = go.GetComponent<T>();
window.Show();
openedWindows[windowName] = window;
onShow?.Invoke(window);
};
}
@@ -33,5 +34,20 @@ public class UIManager : SingletonMono<UIManager>
{
openedWindows[windowName].Hide();
}
else
{
Debug.Log(<>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>ڣ<EFBFBD>ʹ<EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
}
public bool CheckShow(string windowName)
{
if (openedWindows.ContainsKey(windowName))
{
return openedWindows[windowName].gameObject.activeSelf;
}
else
{
return false;
}
}
}