这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

59 行
2.1 KiB

using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Rendering.Universal;
public class SettingsSystem : MonoBehaviour
{
[SerializeField] private VoidEventChannelSO SaveSettingsEvent = default;
[SerializeField] private SettingsSO _currentSettings = default;
[SerializeField] private UniversalRenderPipelineAsset _urpAsset = default;
[SerializeField] private SaveSystem _saveSystem = default;
[SerializeField] private FloatEventChannelSO _changeMasterVolumeEventChannel = default;
[SerializeField] private FloatEventChannelSO _changeSFXVolumeEventChannel = default;
[SerializeField] private FloatEventChannelSO _changeMusicVolumeEventChannel = default;
private void Awake()
{
_saveSystem.LoadSaveDataFromDisk();
_currentSettings.LoadSavedSettings(_saveSystem.saveData);
SetCurrentSettings();
}
private void OnEnable()
{
SaveSettingsEvent.OnEventRaised += SaveSettings;
}
private void OnDisable()
{
SaveSettingsEvent.OnEventRaised -= SaveSettings;
}
/// <summary>
/// Set current settings
/// </summary>
void SetCurrentSettings()
{
_changeMusicVolumeEventChannel.RaiseEvent(_currentSettings.MusicVolume);//raise event for volume change
_changeSFXVolumeEventChannel.RaiseEvent(_currentSettings.SfxVolume); //raise event for volume change
_changeMasterVolumeEventChannel.RaiseEvent(_currentSettings.MasterVolume); //raise event for volume change
Resolution currentResolution = Screen.currentResolution; // get a default resolution in case saved resolution doesn't exist in the resolution List
if (_currentSettings.ResolutionsIndex < Screen.resolutions.Length)
currentResolution = Screen.resolutions[_currentSettings.ResolutionsIndex];
Screen.SetResolution(currentResolution.width, currentResolution.height, _currentSettings.IsFullscreen);
_urpAsset.shadowDistance = _currentSettings.ShadowDistance;
_urpAsset.msaaSampleCount = _currentSettings.AntiAliasingIndex;
LocalizationSettings.SelectedLocale = _currentSettings.CurrentLocale;
}
void SaveSettings()
{
_saveSystem.SaveDataToDisk();
}
}