| | 1 | | using UnityEngine; |
| | 2 | | using System; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using UnityEngine.Audio; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class Settings : DCL.Singleton<Settings> |
| | 10 | | { |
| | 11 | | const string QUALITY_SETTINGS_KEY = "Settings.Quality"; |
| | 12 | | const string GENERAL_SETTINGS_KEY = "Settings.General"; |
| | 13 | | const string AUDIO_SETTINGS_KEY = "Settings.Audio"; |
| | 14 | |
|
| | 15 | | public event Action<SettingsData.QualitySettings> OnQualitySettingsChanged; |
| | 16 | | public event Action<SettingsData.GeneralSettings> OnGeneralSettingsChanged; |
| | 17 | | public event Action<SettingsData.AudioSettings> OnAudioSettingsChanged; |
| | 18 | | public event Action OnResetAllSettings; |
| | 19 | |
|
| 0 | 20 | | public SettingsData.QualitySettings qualitySettings { get { return currentQualitySettings; } } |
| 0 | 21 | | public SettingsData.QualitySettingsData qualitySettingsPresets { get { return qualitySettingsPreset; } } |
| 0 | 22 | | public SettingsData.GeneralSettings generalSettings { get { return currentGeneralSettings; } } |
| 0 | 23 | | public SettingsData.AudioSettings audioSettings { get { return currentAudioSettings; } } |
| | 24 | |
|
| | 25 | | private static SettingsData.QualitySettingsData qualitySettingsPreset = null; |
| | 26 | |
|
| | 27 | | public SettingsData.QualitySettingsData autoqualitySettings = null; |
| | 28 | | public SettingsData.QualitySettings lastValidAutoqualitySet; |
| | 29 | |
|
| | 30 | | private readonly BooleanVariable autosettingsEnabled = null; |
| | 31 | |
|
| 0 | 32 | | public SettingsData.QualitySettings currentQualitySettings { private set; get; } |
| | 33 | | private SettingsData.GeneralSettings currentGeneralSettings; |
| 0 | 34 | | public SettingsData.AudioSettings currentAudioSettings { private set; get; } |
| | 35 | |
|
| | 36 | | AudioMixer audioMixer; |
| | 37 | |
|
| 1 | 38 | | public Settings() |
| | 39 | | { |
| 1 | 40 | | if (qualitySettingsPreset == null) |
| | 41 | | { |
| 1 | 42 | | qualitySettingsPreset = Resources.Load<SettingsData.QualitySettingsData>("ScriptableObjects/QualitySetti |
| | 43 | | } |
| | 44 | |
|
| 1 | 45 | | if (autoqualitySettings == null) |
| | 46 | | { |
| 1 | 47 | | autoqualitySettings = Resources.Load<SettingsData.QualitySettingsData>("ScriptableObjects/AutoQualitySet |
| 1 | 48 | | lastValidAutoqualitySet = autoqualitySettings[autoqualitySettings.Length / 2]; |
| | 49 | | } |
| | 50 | |
|
| 1 | 51 | | if (autosettingsEnabled == null) |
| 1 | 52 | | autosettingsEnabled = Resources.Load<BooleanVariable>("ScriptableObjects/AutoQualityEnabled"); |
| | 53 | |
|
| 1 | 54 | | LoadQualitySettings(); |
| 1 | 55 | | LoadGeneralSettings(); |
| 1 | 56 | | LoadAudioSettings(); |
| | 57 | |
|
| 1 | 58 | | SubscribeToVirtualAudioMixerEvents(); |
| 1 | 59 | | audioMixer = Resources.Load<AudioMixer>("AudioMixer"); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | public void Dispose() { |
| 2 | 63 | | UnsubscribeFromVirtualAudioMixerEvents(); |
| 2 | 64 | | } |
| | 65 | |
|
| | 66 | | private void LoadQualitySettings() |
| | 67 | | { |
| 1 | 68 | | bool isQualitySettingsSet = false; |
| 1 | 69 | | if (PlayerPrefsUtils.HasKey(QUALITY_SETTINGS_KEY)) |
| | 70 | | { |
| | 71 | | try |
| | 72 | | { |
| 0 | 73 | | currentQualitySettings = JsonUtility.FromJson<SettingsData.QualitySettings>(PlayerPrefsUtils.GetStri |
| 0 | 74 | | isQualitySettingsSet = true; |
| 0 | 75 | | } |
| | 76 | | catch (Exception e) |
| | 77 | | { |
| 0 | 78 | | Debug.LogError(e.Message); |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 1 | 82 | | if (!isQualitySettingsSet) |
| | 83 | | { |
| 1 | 84 | | currentQualitySettings = qualitySettingsPreset.defaultPreset; |
| | 85 | | } |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | private void LoadGeneralSettings() |
| | 89 | | { |
| 1 | 90 | | currentGeneralSettings = GetDefaultGeneralSettings(); |
| | 91 | |
|
| 1 | 92 | | if (PlayerPrefsUtils.HasKey(GENERAL_SETTINGS_KEY)) |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| 0 | 96 | | object currentSetting = currentGeneralSettings; |
| 0 | 97 | | JsonUtility.FromJsonOverwrite(PlayerPrefsUtils.GetString(GENERAL_SETTINGS_KEY), currentSetting); |
| 0 | 98 | | currentGeneralSettings = (SettingsData.GeneralSettings)currentSetting; |
| 0 | 99 | | } |
| | 100 | | catch (Exception e) |
| | 101 | | { |
| 0 | 102 | | Debug.LogError(e.Message); |
| 0 | 103 | | } |
| | 104 | | } |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | private void LoadAudioSettings() |
| | 108 | | { |
| 1 | 109 | | currentAudioSettings = GetDefaultAudioSettings(); |
| | 110 | |
|
| 1 | 111 | | if (PlayerPrefsUtils.HasKey(AUDIO_SETTINGS_KEY)) |
| | 112 | | { |
| | 113 | | try |
| | 114 | | { |
| 0 | 115 | | object currentSetting = currentAudioSettings; |
| 0 | 116 | | JsonUtility.FromJsonOverwrite(PlayerPrefsUtils.GetString(AUDIO_SETTINGS_KEY), currentSetting); |
| 0 | 117 | | currentAudioSettings = (SettingsData.AudioSettings)currentSetting; |
| 0 | 118 | | } |
| | 119 | | catch (Exception e) |
| | 120 | | { |
| 0 | 121 | | Debug.LogError(e.Message); |
| 0 | 122 | | } |
| | 123 | | } |
| 1 | 124 | | } |
| | 125 | |
|
| | 126 | | public void LoadDefaultSettings() |
| | 127 | | { |
| 0 | 128 | | autosettingsEnabled.Set(false); |
| 0 | 129 | | currentQualitySettings = qualitySettingsPreset.defaultPreset; |
| 0 | 130 | | currentGeneralSettings = GetDefaultGeneralSettings(); |
| 0 | 131 | | currentAudioSettings = GetDefaultAudioSettings(); |
| | 132 | |
|
| 0 | 133 | | ApplyQualitySettings(currentQualitySettings); |
| 0 | 134 | | ApplyGeneralSettings(currentGeneralSettings); |
| 0 | 135 | | ApplyAudioSettings(currentAudioSettings); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void ResetAllSettings() |
| | 139 | | { |
| 0 | 140 | | LoadDefaultSettings(); |
| 0 | 141 | | SaveSettings(); |
| 0 | 142 | | OnResetAllSettings?.Invoke(); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | private SettingsData.GeneralSettings GetDefaultGeneralSettings() |
| | 146 | | { |
| 1 | 147 | | return new SettingsData.GeneralSettings() |
| | 148 | | { |
| | 149 | | mouseSensitivity = 0.6f, |
| | 150 | | scenesLoadRadius = 4, |
| | 151 | | avatarsLODDistance = 16, |
| | 152 | | maxNonLODAvatars = 20, |
| | 153 | | voiceChatVolume = 1, |
| | 154 | | voiceChatAllow = SettingsData.GeneralSettings.VoiceChatAllow.ALL_USERS, |
| | 155 | | autoqualityOn = false |
| | 156 | | }; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | private SettingsData.AudioSettings GetDefaultAudioSettings() |
| | 160 | | { |
| 1 | 161 | | return new SettingsData.AudioSettings() |
| | 162 | | { |
| | 163 | | masterVolume = 1f, |
| | 164 | | voiceChatVolume = 1f, |
| | 165 | | avatarSFXVolume = 1f, |
| | 166 | | uiSFXVolume = 1f, |
| | 167 | | sceneSFXVolume = 1f, |
| | 168 | | musicVolume = 1f, |
| | 169 | | chatSFXEnabled = true |
| | 170 | | }; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Apply the auto quality setting by its index on the array |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="index">Index within the autoQualitySettings array</param> |
| | 177 | | public void ApplyAutoQualitySettings(int index) |
| | 178 | | { |
| 0 | 179 | | if (index < 0 || index >= autoqualitySettings.Length) |
| 0 | 180 | | return; |
| | 181 | |
|
| 0 | 182 | | lastValidAutoqualitySet = autoqualitySettings[index]; |
| 0 | 183 | | lastValidAutoqualitySet.baseResolution = currentQualitySettings.baseResolution; |
| 0 | 184 | | lastValidAutoqualitySet.fpsCap = currentQualitySettings.fpsCap; |
| | 185 | |
|
| 0 | 186 | | if (currentQualitySettings.Equals(lastValidAutoqualitySet)) |
| 0 | 187 | | return; |
| | 188 | |
|
| 0 | 189 | | ApplyQualitySettings(lastValidAutoqualitySet); |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | public void ApplyQualitySettings(SettingsData.QualitySettings settings) |
| | 193 | | { |
| 1 | 194 | | if (settings.Equals(currentQualitySettings)) |
| 0 | 195 | | return; |
| | 196 | |
|
| 1 | 197 | | currentQualitySettings = settings; |
| 1 | 198 | | OnQualitySettingsChanged?.Invoke(settings); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void ApplyGeneralSettings(SettingsData.GeneralSettings settings) |
| | 202 | | { |
| 0 | 203 | | if (settings.Equals(currentGeneralSettings)) |
| 0 | 204 | | return; |
| | 205 | |
|
| 0 | 206 | | currentGeneralSettings = settings; |
| 0 | 207 | | OnGeneralSettingsChanged?.Invoke(settings); |
| 0 | 208 | | autosettingsEnabled.Set(settings.autoqualityOn); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | public void ApplyAudioSettings(SettingsData.AudioSettings settings) |
| | 212 | | { |
| 0 | 213 | | if (settings.Equals(currentAudioSettings)) |
| 0 | 214 | | return; |
| | 215 | |
|
| 0 | 216 | | currentAudioSettings = settings; |
| 0 | 217 | | OnAudioSettingsChanged?.Invoke(settings); |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | private void SubscribeToVirtualAudioMixerEvents() { |
| 1 | 221 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange += ApplyVoiceChatSettings; |
| 1 | 222 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange += ApplyMusicVolume; |
| 1 | 223 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange += ApplyAvatarSFXVolume; |
| 1 | 224 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange += ApplyUISFXVolume; |
| 1 | 225 | | } |
| | 226 | |
|
| | 227 | | private void UnsubscribeFromVirtualAudioMixerEvents() { |
| 2 | 228 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange -= ApplyVoiceChatSettings; |
| 2 | 229 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange -= ApplyMusicVolume; |
| 2 | 230 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange -= ApplyAvatarSFXVolume; |
| 2 | 231 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange -= ApplyUISFXVolume; |
| 2 | 232 | | } |
| | 233 | |
|
| | 234 | | public void ApplyMasterVolume() { |
| | 235 | | // Update the "All" mixer group |
| 2 | 236 | | audioMixer.SetFloat("AllBusVolume", Utils.ToAudioMixerGroupVolume(currentAudioSettings.masterVolume)); |
| | 237 | |
|
| | 238 | | // Update voice chat volume, as it does not pass through the AudioMixer |
| 2 | 239 | | ApplyVoiceChatSettings(); |
| 2 | 240 | | } |
| | 241 | |
|
| | 242 | | public void ApplyVoiceChatSettings(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { |
| 6 | 243 | | float calculatedVolume = Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.voiceChatVolume.Get() * currentAu |
| 6 | 244 | | WebInterface.ApplySettings(calculatedVolume, (int)currentGeneralSettings.voiceChatAllow); |
| 6 | 245 | | } |
| | 246 | |
|
| | 247 | | public void ApplyAvatarSFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { |
| 2 | 248 | | audioMixer.SetFloat("AvatarSFXBusVolume", Utils.ToAudioMixerGroupVolume(DataStore.i.virtualAudioMixer.avatar |
| 2 | 249 | | } |
| | 250 | |
|
| | 251 | | public void ApplyUISFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { |
| 2 | 252 | | audioMixer.SetFloat("UIBusVolume", Utils.ToAudioMixerGroupVolume(DataStore.i.virtualAudioMixer.uiSFXVolume.G |
| 2 | 253 | | } |
| | 254 | |
|
| | 255 | | public void ApplyMusicVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { |
| 2 | 256 | | audioMixer.SetFloat("MusicBusVolume", Utils.ToAudioMixerGroupVolume(DataStore.i.virtualAudioMixer.uiSFXVolum |
| 2 | 257 | | } |
| | 258 | |
|
| | 259 | | public void SaveSettings() |
| | 260 | | { |
| 0 | 261 | | PlayerPrefsUtils.SetString(GENERAL_SETTINGS_KEY, JsonUtility.ToJson(currentGeneralSettings)); |
| 0 | 262 | | PlayerPrefsUtils.SetString(QUALITY_SETTINGS_KEY, JsonUtility.ToJson(currentQualitySettings)); |
| 0 | 263 | | PlayerPrefsUtils.SetString(AUDIO_SETTINGS_KEY, JsonUtility.ToJson(currentAudioSettings)); |
| 0 | 264 | | PlayerPrefsUtils.Save(); |
| 0 | 265 | | } |
| | 266 | | } |
| | 267 | | } |
| | 268 | |
|
| | 269 | | namespace DCL.SettingsData |
| | 270 | | { |
| | 271 | | [Serializable] |
| | 272 | | public struct GeneralSettings |
| | 273 | | { |
| | 274 | | public enum VoiceChatAllow { ALL_USERS, VERIFIED_ONLY, FRIENDS_ONLY } |
| | 275 | |
|
| | 276 | | public float mouseSensitivity; |
| | 277 | | public float voiceChatVolume; |
| | 278 | | public VoiceChatAllow voiceChatAllow; |
| | 279 | | public bool autoqualityOn; |
| | 280 | | public float scenesLoadRadius; |
| | 281 | | public float avatarsLODDistance; |
| | 282 | | public float maxNonLODAvatars; |
| | 283 | |
|
| | 284 | | public bool Equals(GeneralSettings settings) |
| | 285 | | { |
| | 286 | | return mouseSensitivity == settings.mouseSensitivity |
| | 287 | | && scenesLoadRadius == settings.scenesLoadRadius |
| | 288 | | && avatarsLODDistance == settings.avatarsLODDistance |
| | 289 | | && maxNonLODAvatars == settings.maxNonLODAvatars |
| | 290 | | && voiceChatVolume == settings.voiceChatVolume |
| | 291 | | && voiceChatAllow == settings.voiceChatAllow |
| | 292 | | && autoqualityOn == settings.autoqualityOn; |
| | 293 | | } |
| | 294 | | } |
| | 295 | |
|
| | 296 | | [Serializable] |
| | 297 | | public struct AudioSettings |
| | 298 | | { |
| | 299 | | public float masterVolume; |
| | 300 | | public float voiceChatVolume; |
| | 301 | | public float avatarSFXVolume; |
| | 302 | | public float uiSFXVolume; |
| | 303 | | public float sceneSFXVolume; // Note(Mordi): Also known as "World SFX" |
| | 304 | | public float musicVolume; |
| | 305 | | public bool chatSFXEnabled; |
| | 306 | |
|
| | 307 | | public bool Equals(AudioSettings settings) { |
| | 308 | | return masterVolume == settings.masterVolume |
| | 309 | | && voiceChatVolume == settings.voiceChatVolume |
| | 310 | | && avatarSFXVolume == settings.avatarSFXVolume |
| | 311 | | && uiSFXVolume == settings.uiSFXVolume |
| | 312 | | && sceneSFXVolume == settings.sceneSFXVolume |
| | 313 | | && musicVolume == settings.musicVolume |
| | 314 | | && chatSFXEnabled == settings.chatSFXEnabled; |
| | 315 | | } |
| | 316 | | } |
| | 317 | | } |