| | 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 | |
|
| 4 | 62 | | public void Dispose() { UnsubscribeFromVirtualAudioMixerEvents(); } |
| | 63 | |
|
| | 64 | | private void LoadQualitySettings() |
| | 65 | | { |
| 1 | 66 | | bool isQualitySettingsSet = false; |
| 1 | 67 | | if (PlayerPrefsUtils.HasKey(QUALITY_SETTINGS_KEY)) |
| | 68 | | { |
| | 69 | | try |
| | 70 | | { |
| 0 | 71 | | currentQualitySettings = JsonUtility.FromJson<SettingsData.QualitySettings>(PlayerPrefsUtils.GetStri |
| 0 | 72 | | isQualitySettingsSet = true; |
| 0 | 73 | | } |
| | 74 | | catch (Exception e) |
| | 75 | | { |
| 0 | 76 | | Debug.LogError(e.Message); |
| 0 | 77 | | } |
| | 78 | | } |
| | 79 | |
|
| 1 | 80 | | if (!isQualitySettingsSet) |
| | 81 | | { |
| 1 | 82 | | currentQualitySettings = qualitySettingsPreset.defaultPreset; |
| | 83 | | } |
| 1 | 84 | | } |
| | 85 | |
|
| | 86 | | private void LoadGeneralSettings() |
| | 87 | | { |
| 1 | 88 | | currentGeneralSettings = GetDefaultGeneralSettings(); |
| | 89 | |
|
| 1 | 90 | | if (PlayerPrefsUtils.HasKey(GENERAL_SETTINGS_KEY)) |
| | 91 | | { |
| | 92 | | try |
| | 93 | | { |
| 0 | 94 | | object currentSetting = currentGeneralSettings; |
| 0 | 95 | | JsonUtility.FromJsonOverwrite(PlayerPrefsUtils.GetString(GENERAL_SETTINGS_KEY), currentSetting); |
| 0 | 96 | | currentGeneralSettings = (SettingsData.GeneralSettings)currentSetting; |
| 0 | 97 | | } |
| | 98 | | catch (Exception e) |
| | 99 | | { |
| 0 | 100 | | Debug.LogError(e.Message); |
| 0 | 101 | | } |
| | 102 | | } |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | private void LoadAudioSettings() |
| | 106 | | { |
| 1 | 107 | | currentAudioSettings = GetDefaultAudioSettings(); |
| | 108 | |
|
| 1 | 109 | | if (PlayerPrefsUtils.HasKey(AUDIO_SETTINGS_KEY)) |
| | 110 | | { |
| | 111 | | try |
| | 112 | | { |
| 0 | 113 | | object currentSetting = currentAudioSettings; |
| 0 | 114 | | JsonUtility.FromJsonOverwrite(PlayerPrefsUtils.GetString(AUDIO_SETTINGS_KEY), currentSetting); |
| 0 | 115 | | currentAudioSettings = (SettingsData.AudioSettings)currentSetting; |
| 0 | 116 | | } |
| | 117 | | catch (Exception e) |
| | 118 | | { |
| 0 | 119 | | Debug.LogError(e.Message); |
| 0 | 120 | | } |
| | 121 | | } |
| 1 | 122 | | } |
| | 123 | |
|
| | 124 | | public void LoadDefaultSettings() |
| | 125 | | { |
| 0 | 126 | | autosettingsEnabled.Set(false); |
| 0 | 127 | | currentQualitySettings = qualitySettingsPreset.defaultPreset; |
| 0 | 128 | | currentGeneralSettings = GetDefaultGeneralSettings(); |
| 0 | 129 | | currentAudioSettings = GetDefaultAudioSettings(); |
| | 130 | |
|
| 0 | 131 | | ApplyQualitySettings(currentQualitySettings); |
| 0 | 132 | | ApplyGeneralSettings(currentGeneralSettings); |
| 0 | 133 | | ApplyAudioSettings(currentAudioSettings); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public void ResetAllSettings() |
| | 137 | | { |
| 0 | 138 | | LoadDefaultSettings(); |
| 0 | 139 | | SaveSettings(); |
| 0 | 140 | | OnResetAllSettings?.Invoke(); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private SettingsData.GeneralSettings GetDefaultGeneralSettings() |
| | 144 | | { |
| 1 | 145 | | return new SettingsData.GeneralSettings() |
| | 146 | | { |
| | 147 | | mouseSensitivity = 0.6f, |
| | 148 | | scenesLoadRadius = 4, |
| | 149 | | avatarsLODDistance = 16, |
| | 150 | | maxNonLODAvatars = DataStore.DataStore_AvatarsLOD.DEFAULT_MAX_AVATAR, |
| | 151 | | voiceChatVolume = 1, |
| | 152 | | voiceChatAllow = SettingsData.GeneralSettings.VoiceChatAllow.ALL_USERS, |
| | 153 | | autoqualityOn = false |
| | 154 | | }; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | private SettingsData.AudioSettings GetDefaultAudioSettings() |
| | 158 | | { |
| 1 | 159 | | return new SettingsData.AudioSettings() |
| | 160 | | { |
| | 161 | | masterVolume = 1f, |
| | 162 | | voiceChatVolume = 1f, |
| | 163 | | avatarSFXVolume = 1f, |
| | 164 | | uiSFXVolume = 1f, |
| | 165 | | sceneSFXVolume = 1f, |
| | 166 | | musicVolume = 1f, |
| | 167 | | chatSFXEnabled = true |
| | 168 | | }; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Apply the auto quality setting by its index on the array |
| | 173 | | /// </summary> |
| | 174 | | /// <param name="index">Index within the autoQualitySettings array</param> |
| | 175 | | public void ApplyAutoQualitySettings(int index) |
| | 176 | | { |
| 0 | 177 | | if (index < 0 || index >= autoqualitySettings.Length) |
| 0 | 178 | | return; |
| | 179 | |
|
| 0 | 180 | | lastValidAutoqualitySet = autoqualitySettings[index]; |
| 0 | 181 | | lastValidAutoqualitySet.baseResolution = currentQualitySettings.baseResolution; |
| 0 | 182 | | lastValidAutoqualitySet.fpsCap = currentQualitySettings.fpsCap; |
| | 183 | |
|
| 0 | 184 | | if (currentQualitySettings.Equals(lastValidAutoqualitySet)) |
| 0 | 185 | | return; |
| | 186 | |
|
| 0 | 187 | | ApplyQualitySettings(lastValidAutoqualitySet); |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | public void ApplyQualitySettings(SettingsData.QualitySettings settings) |
| | 191 | | { |
| 1 | 192 | | if (settings.Equals(currentQualitySettings)) |
| 0 | 193 | | return; |
| | 194 | |
|
| 1 | 195 | | currentQualitySettings = settings; |
| 1 | 196 | | OnQualitySettingsChanged?.Invoke(settings); |
| 1 | 197 | | } |
| | 198 | |
|
| | 199 | | public void ApplyGeneralSettings(SettingsData.GeneralSettings settings) |
| | 200 | | { |
| 0 | 201 | | if (settings.Equals(currentGeneralSettings)) |
| 0 | 202 | | return; |
| | 203 | |
|
| 0 | 204 | | currentGeneralSettings = settings; |
| 0 | 205 | | OnGeneralSettingsChanged?.Invoke(settings); |
| 0 | 206 | | autosettingsEnabled.Set(settings.autoqualityOn); |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | public void ApplyAudioSettings(SettingsData.AudioSettings settings) |
| | 210 | | { |
| 0 | 211 | | if (settings.Equals(currentAudioSettings)) |
| 0 | 212 | | return; |
| | 213 | |
|
| 0 | 214 | | currentAudioSettings = settings; |
| 0 | 215 | | OnAudioSettingsChanged?.Invoke(settings); |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | private void SubscribeToVirtualAudioMixerEvents() |
| | 219 | | { |
| 1 | 220 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange += ApplyVoiceChatSettings; |
| 1 | 221 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange += ApplyMusicVolume; |
| 1 | 222 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange += ApplyAvatarSFXVolume; |
| 1 | 223 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange += ApplyUISFXVolume; |
| 1 | 224 | | } |
| | 225 | |
|
| | 226 | | private void UnsubscribeFromVirtualAudioMixerEvents() |
| | 227 | | { |
| 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 | | { |
| | 236 | | // Update the "All" mixer group |
| 2 | 237 | | audioMixer.SetFloat("AllBusVolume", Utils.ToAudioMixerGroupVolume(currentAudioSettings.masterVolume)); |
| | 238 | |
|
| | 239 | | // Update voice chat volume, as it does not pass through the AudioMixer |
| 2 | 240 | | ApplyVoiceChatSettings(); |
| 2 | 241 | | } |
| | 242 | |
|
| | 243 | | public void ApplyVoiceChatSettings(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) |
| | 244 | | { |
| 6 | 245 | | float calculatedVolume = Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.voiceChatVolume.Get() * currentAu |
| 6 | 246 | | WebInterface.ApplySettings(calculatedVolume, (int)currentGeneralSettings.voiceChatAllow); |
| 6 | 247 | | } |
| | 248 | |
|
| 4 | 249 | | public void ApplyAvatarSFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioM |
| | 250 | |
|
| 4 | 251 | | public void ApplyUISFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 252 | |
|
| 4 | 253 | | public void ApplyMusicVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 254 | |
|
| | 255 | | public void SaveSettings() |
| | 256 | | { |
| 0 | 257 | | PlayerPrefsUtils.SetString(GENERAL_SETTINGS_KEY, JsonUtility.ToJson(currentGeneralSettings)); |
| 0 | 258 | | PlayerPrefsUtils.SetString(QUALITY_SETTINGS_KEY, JsonUtility.ToJson(currentQualitySettings)); |
| 0 | 259 | | PlayerPrefsUtils.SetString(AUDIO_SETTINGS_KEY, JsonUtility.ToJson(currentAudioSettings)); |
| 0 | 260 | | PlayerPrefsUtils.Save(); |
| 0 | 261 | | } |
| | 262 | | } |
| | 263 | | } |
| | 264 | |
|
| | 265 | | namespace DCL.SettingsData |
| | 266 | | { |
| | 267 | | [Serializable] |
| | 268 | | public struct GeneralSettings |
| | 269 | | { |
| | 270 | | public enum VoiceChatAllow { ALL_USERS, VERIFIED_ONLY, FRIENDS_ONLY } |
| | 271 | |
|
| | 272 | | public float mouseSensitivity; |
| | 273 | | public float voiceChatVolume; |
| | 274 | | public VoiceChatAllow voiceChatAllow; |
| | 275 | | public bool autoqualityOn; |
| | 276 | | public float scenesLoadRadius; |
| | 277 | | public float avatarsLODDistance; |
| | 278 | | public float maxNonLODAvatars; |
| | 279 | |
|
| | 280 | | public bool Equals(GeneralSettings settings) |
| | 281 | | { |
| | 282 | | return mouseSensitivity == settings.mouseSensitivity |
| | 283 | | && scenesLoadRadius == settings.scenesLoadRadius |
| | 284 | | && avatarsLODDistance == settings.avatarsLODDistance |
| | 285 | | && maxNonLODAvatars == settings.maxNonLODAvatars |
| | 286 | | && voiceChatVolume == settings.voiceChatVolume |
| | 287 | | && voiceChatAllow == settings.voiceChatAllow |
| | 288 | | && autoqualityOn == settings.autoqualityOn; |
| | 289 | | } |
| | 290 | | } |
| | 291 | |
|
| | 292 | | [Serializable] |
| | 293 | | public struct AudioSettings |
| | 294 | | { |
| | 295 | | public float masterVolume; |
| | 296 | | public float voiceChatVolume; |
| | 297 | | public float avatarSFXVolume; |
| | 298 | | public float uiSFXVolume; |
| | 299 | | public float sceneSFXVolume; // Note(Mordi): Also known as "World SFX" |
| | 300 | | public float musicVolume; |
| | 301 | | public bool chatSFXEnabled; |
| | 302 | |
|
| | 303 | | public bool Equals(AudioSettings settings) |
| | 304 | | { |
| | 305 | | return masterVolume == settings.masterVolume |
| | 306 | | && voiceChatVolume == settings.voiceChatVolume |
| | 307 | | && avatarSFXVolume == settings.avatarSFXVolume |
| | 308 | | && uiSFXVolume == settings.uiSFXVolume |
| | 309 | | && sceneSFXVolume == settings.sceneSFXVolume |
| | 310 | | && musicVolume == settings.musicVolume |
| | 311 | | && chatSFXEnabled == settings.chatSFXEnabled; |
| | 312 | | } |
| | 313 | | } |
| | 314 | | } |