Skip to content

Commit c5d61dd

Browse files
committed
Made a refactor of the used setting classes as follows:
- Logic is now split into sub components: Genera, Graphics, Audio and Input (currently empty as input system is wip) - The settings system class can be used to get information from all sub components, a distributor - Fixed an issue with Screen resolution not being correct when in windowed mode (Screen.currentResolution gives back desktop resolution when in windowed mode) - Added Input UI elements which are placeholders for now, once the input system is done this can be populated like the other settings sub components
1 parent 170e955 commit c5d61dd

12 files changed

+976
-337
lines changed

UOP1_Project/Assets/Scenes/Settings System.unity

Lines changed: 578 additions & 90 deletions
Large diffs are not rendered by default.

UOP1_Project/Assets/Scripts/Systems/Settings/SettingsPresetsScriptableObject.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,31 @@ public class SettingsPresetsScriptableObject : ScriptableObject
1111
[Serializable]
1212
public struct AdvancedGraphics
1313
{
14-
public string name;
14+
public GraphicsQualityLevel qualityLevel;
1515
public ShadowQuality shadowQuality;
1616
public AnisotropicFiltering anisotropicFiltering;
1717
public int antiAliasing;
1818
public float shadowDistance;
1919
public bool custom;
2020
}
2121

22-
public string[] GetPresetNames()
22+
public enum GraphicsQualityLevel
2323
{
24-
string[] presetNames = new string[presetList.Count];
25-
for (int i = 0; i < presetList.Count; i++)
24+
Low,
25+
Middle,
26+
High
27+
}
28+
29+
public AdvancedGraphics GetPresetByQualityLevel(GraphicsQualityLevel level)
30+
{
31+
foreach (AdvancedGraphics preset in presetList)
2632
{
27-
presetNames[i] = presetList[i].name;
33+
if (level == preset.qualityLevel)
34+
{
35+
return preset;
36+
}
2837
}
2938

30-
return presetNames;
39+
return default;
3140
}
3241
}
Lines changed: 8 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -1,244 +1,14 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using TMPro;
6-
using UnityEngine;
7-
using UnityEngine.Rendering;
8-
using UnityEngine.UI;
1+
using UnityEngine;
92

103
public class SettingsSystem : MonoBehaviour
114
{
12-
[SerializeField] TMP_Dropdown resolutionsDropdown;
13-
[SerializeField] TMP_Dropdown languageDropdown;
14-
[SerializeField] TMP_Dropdown shadowQualityDropdown;
15-
[SerializeField] TMP_Dropdown anisotropicFilteringDropdown;
16-
[SerializeField] TMP_Dropdown qualityPresetsDropdown;
17-
[SerializeField] Slider antiAliasingSlider;
18-
[SerializeField] Slider shadowDistanceSlider;
19-
[SerializeField] Slider musicVolumeSlider;
20-
[SerializeField] Slider sfxVolumeSlider;
21-
[SerializeField] TextMeshProUGUI antiAliasingText;
22-
[SerializeField] TextMeshProUGUI shadowDistanceText;
23-
[SerializeField] SettingsPresetsScriptableObject settingsPresets;
5+
[SerializeField] SettingsSystemGeneralComponent generalComponent;
6+
[SerializeField] SettingsSystemGraphicsComponent graphicsComponent;
7+
[SerializeField] SettingsSystemAudioComponent audioComponent;
248

25-
public bool FullScreen { get; private set; }
26-
public float MusicVolume { get; private set; }
27-
public float SfxVolume { get; private set; }
28-
public LanguageSetting Language { get; private set; }
9+
public SettingsSystemGeneralComponent.LanguageSetting Language => generalComponent.Language;
10+
public bool FullScreen => graphicsComponent.FullScreen;
11+
public float MusicVolume => audioComponent.MusicVolume;
12+
public float SfxVolume => audioComponent.SfxVolume;
2913

30-
SettingsPresetsScriptableObject.AdvancedGraphics currentAdvancedGraphics, previousAdvancedGraphics;
31-
int currentQualityLevel, previousQualityLevel; //-1 is custom
32-
33-
public enum LanguageSetting
34-
{
35-
English,
36-
German,
37-
//TODO: which languages are going to be supported?
38-
}
39-
40-
void Start()
41-
{
42-
//TODO: Load previous settings data via save/load interface
43-
resolutionsDropdown.AddOptions(GetResolutionsDropdownData());
44-
languageDropdown.AddOptions(GetDropdownData(Enum.GetNames(typeof(LanguageSetting))));
45-
qualityPresetsDropdown.AddOptions(GetDropdownData(settingsPresets.GetPresetNames(), "Custom"));
46-
foreach (string s in QualitySettings.names)
47-
{
48-
Debug.Log(s);
49-
}
50-
anisotropicFilteringDropdown.AddOptions(GetDropdownData(Enum.GetNames(typeof(AnisotropicFiltering))));
51-
shadowQualityDropdown.AddOptions(GetDropdownData(Enum.GetNames(typeof(ShadowQuality))));
52-
LoadCurrentSettings();
53-
}
54-
55-
void LoadCurrentSettings()
56-
{
57-
int resolutionIndex = 0;
58-
//TODO: load previous resolution setting. If not existing find current resolution index
59-
for (int i = 0; i < Screen.resolutions.Length; i++)
60-
{
61-
if (Screen.currentResolution.ToString() == Screen.resolutions[i].ToString())
62-
{
63-
resolutionIndex = i;
64-
}
65-
}
66-
67-
resolutionsDropdown.SetValueWithoutNotify(resolutionIndex);
68-
//TODO: load quality level from previous session. If custom, set qualityPresetsDropdown to custom. Option "custom" is added in GetQualityPresetsDropdownData()
69-
previousQualityLevel = currentQualityLevel;
70-
currentAdvancedGraphics = settingsPresets.presetList[currentQualityLevel]; //Set to lowest preset initially
71-
previousAdvancedGraphics = currentAdvancedGraphics;
72-
qualityPresetsDropdown.SetValueWithoutNotify(currentAdvancedGraphics.custom ? qualityPresetsDropdown.options.Count-1 : currentQualityLevel);
73-
74-
UpdateAdvancedGraphicsUI();
75-
76-
//TODO: load previous language setting
77-
Language = LanguageSetting.English;
78-
languageDropdown.SetValueWithoutNotify((int) Language);
79-
//TODO: load previous fullscreen setting
80-
Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
81-
//TODO: load previous music volume setting
82-
MusicVolume = 0.5f;
83-
musicVolumeSlider.SetValueWithoutNotify(MusicVolume);
84-
//TODO: load previous sfx volume setting
85-
SfxVolume = 0.5f;
86-
sfxVolumeSlider.SetValueWithoutNotify(SfxVolume);
87-
}
88-
89-
void SelectGraphicsPreset(int level)
90-
{
91-
QualitySettings.SetQualityLevel(0, true);
92-
UpdateAdvancedGraphicsUI();
93-
}
94-
95-
void UpdateAdvancedGraphicsUI()
96-
{
97-
anisotropicFilteringDropdown.SetValueWithoutNotify((int) currentAdvancedGraphics.anisotropicFiltering);
98-
antiAliasingSlider.SetValueWithoutNotify(currentAdvancedGraphics.antiAliasing);
99-
shadowDistanceSlider.SetValueWithoutNotify(currentAdvancedGraphics.shadowDistance);
100-
shadowQualityDropdown.SetValueWithoutNotify((int) currentAdvancedGraphics.shadowQuality);
101-
shadowDistanceText.text = currentAdvancedGraphics.shadowDistance.ToString();
102-
antiAliasingText.text = currentAdvancedGraphics.antiAliasing.ToString();
103-
104-
qualityPresetsDropdown.SetValueWithoutNotify(currentAdvancedGraphics.custom ? qualityPresetsDropdown.options.Count-1 : currentQualityLevel);
105-
}
106-
107-
List<TMP_Dropdown.OptionData> GetResolutionsDropdownData()
108-
{
109-
List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
110-
for (int i = 0; i < Screen.resolutions.Length; i++)
111-
{
112-
options.Add(new TMP_Dropdown.OptionData(Screen.resolutions[i].ToString()));
113-
}
114-
115-
return options;
116-
}
117-
118-
List<TMP_Dropdown.OptionData> GetDropdownData(string[] optionNames, params string[] customOptions)
119-
{
120-
List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
121-
foreach (string option in optionNames)
122-
{
123-
options.Add(new TMP_Dropdown.OptionData(option));
124-
}
125-
126-
foreach (string option in customOptions)
127-
{
128-
options.Add(new TMP_Dropdown.OptionData(option));
129-
}
130-
return options;
131-
}
132-
133-
#region GENERAL SETTINGS
134-
135-
public void OnChangeLanguage(int languageIndex)
136-
{
137-
Language = (LanguageSetting) languageIndex;
138-
Debug.Log("Language set to: " + Language);
139-
}
140-
141-
#endregion
142-
143-
#region GRAPHICS SETTINGS
144-
145-
public void OnChangeFullscreen(bool fullScreen)
146-
{
147-
Screen.fullScreenMode = fullScreen ? FullScreenMode.ExclusiveFullScreen : FullScreenMode.Windowed;
148-
}
149-
150-
public void OnChangeResolution(int resolutionIndex)
151-
{
152-
Resolution newResolution = Screen.resolutions[resolutionIndex];
153-
Screen.SetResolution(newResolution.width, newResolution.height, Screen.fullScreenMode);
154-
}
155-
156-
public void OnChangeAnisotropicFiltering(int anisoLevel)
157-
{
158-
currentAdvancedGraphics.anisotropicFiltering = (AnisotropicFiltering) anisoLevel;
159-
currentAdvancedGraphics.custom = true;
160-
UpdateAdvancedGraphicsUI();
161-
}
162-
163-
public void OnChangeAntialiasing(float value)
164-
{
165-
currentAdvancedGraphics.antiAliasing = (int) value;
166-
currentAdvancedGraphics.custom = true;
167-
UpdateAdvancedGraphicsUI();
168-
}
169-
170-
public void OnChangeShadowDistance(float shadowDistanceValue)
171-
{
172-
//TODO: configure min max value in slider
173-
currentAdvancedGraphics.shadowDistance = shadowDistanceValue;
174-
currentAdvancedGraphics.custom = true;
175-
UpdateAdvancedGraphicsUI();
176-
}
177-
178-
public void OnChangeShadowQuality(int level)
179-
{
180-
currentAdvancedGraphics.shadowQuality = (ShadowQuality) level;
181-
currentAdvancedGraphics.custom = true;
182-
UpdateAdvancedGraphicsUI();
183-
}
184-
185-
public void OnChangeQualityPreset(int level)
186-
{
187-
if (level >= settingsPresets.presetList.Count)
188-
{
189-
//Custom level chosen
190-
currentAdvancedGraphics.custom = true;
191-
}
192-
else
193-
{
194-
currentAdvancedGraphics = settingsPresets.presetList[level];
195-
}
196-
currentQualityLevel = level;
197-
UpdateAdvancedGraphicsUI();
198-
}
199-
200-
#endregion
201-
202-
#region AUDIO SETTINGS
203-
204-
//TODO: clamp volume to [0, 1] or [0, 100]? Change Slider min max value in editor depending on use case
205-
public void OnChangeMusicVolume(float volume)
206-
{
207-
MusicVolume = Mathf.Clamp(volume, 0.0f, 1.0f);
208-
}
209-
210-
//TODO: clamp volume to [0, 1] or [0, 100]? Change Slider min max value in editor depending on use case
211-
public void OnChangeSfxVolume(float volume)
212-
{
213-
SfxVolume = Mathf.Clamp(volume, 0.0f, 1.0f);
214-
}
215-
216-
#endregion
217-
218-
public void OnSaveGraphicsSettings()
219-
{
220-
QualitySettings.anisotropicFiltering = currentAdvancedGraphics.anisotropicFiltering;
221-
QualitySettings.antiAliasing = currentAdvancedGraphics.antiAliasing;
222-
QualitySettings.shadowDistance = currentAdvancedGraphics.shadowDistance;
223-
QualitySettings.shadows = currentAdvancedGraphics.shadowQuality;
224-
225-
previousAdvancedGraphics = currentAdvancedGraphics;
226-
previousQualityLevel = currentQualityLevel;
227-
Debug.Log("Antialiasing: " + QualitySettings.antiAliasing);
228-
Debug.Log("Anisotropic Filtering: " + QualitySettings.anisotropicFiltering);
229-
Debug.Log("Shadow Distance: " + QualitySettings.shadowDistance);
230-
Debug.Log("Shadow Quality: " + QualitySettings.shadows);
231-
}
232-
233-
public void OnCancelGraphicsSettings()
234-
{
235-
currentAdvancedGraphics = previousAdvancedGraphics;
236-
currentQualityLevel = previousQualityLevel;
237-
QualitySettings.anisotropicFiltering = currentAdvancedGraphics.anisotropicFiltering;
238-
QualitySettings.antiAliasing = currentAdvancedGraphics.antiAliasing;
239-
QualitySettings.shadowDistance = currentAdvancedGraphics.shadowDistance;
240-
QualitySettings.shadows = currentAdvancedGraphics.shadowQuality;
241-
242-
UpdateAdvancedGraphicsUI();
243-
}
24414
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
public class SettingsSystemAudioComponent : SettingsSystemSubComponents
7+
{
8+
[SerializeField] Slider musicVolumeSlider;
9+
[SerializeField] Slider sfxVolumeSlider;
10+
11+
public float MusicVolume { get; private set; }
12+
public float SfxVolume { get; private set; }
13+
14+
void Start()
15+
{
16+
Setup();
17+
}
18+
19+
protected override void Setup()
20+
{
21+
//TODO: load previous music volume setting
22+
MusicVolume = 50f;
23+
musicVolumeSlider.SetValueWithoutNotify(MusicVolume);
24+
//TODO: load previous sfx volume setting
25+
SfxVolume = 50f;
26+
sfxVolumeSlider.SetValueWithoutNotify(SfxVolume);
27+
}
28+
29+
#region UI CALLBACKS
30+
31+
//TODO: clamp volume to [0, 1] or [0, 100]? Change Slider min max value in editor depending on use case
32+
public void OnChangeMusicVolume(float volume)
33+
{
34+
MusicVolume = Mathf.Clamp(volume, 0.0f, 100.0f);
35+
}
36+
37+
//TODO: clamp volume to [0, 1] or [0, 100]? Change Slider min max value in editor depending on use case
38+
public void OnChangeSfxVolume(float volume)
39+
{
40+
SfxVolume = Mathf.Clamp(volume, 0.0f, 100.0f);
41+
}
42+
43+
#endregion
44+
}

UOP1_Project/Assets/Scripts/Systems/Settings/SettingsSystemAudioComponent.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using TMPro;
5+
using UnityEngine;
6+
7+
public class SettingsSystemGeneralComponent : SettingsSystemSubComponents
8+
{
9+
[SerializeField] TMP_Dropdown languageDropdown;
10+
11+
SettingsSystem settingsSystem;
12+
public LanguageSetting Language { get; private set; }
13+
14+
public enum LanguageSetting
15+
{
16+
English,
17+
German,
18+
//TODO: which languages are going to be supported?
19+
}
20+
21+
void Start()
22+
{
23+
Setup();
24+
}
25+
26+
protected override void Setup()
27+
{
28+
settingsSystem = transform.parent.GetComponent<SettingsSystem>();
29+
languageDropdown.AddOptions(GetDropdownData(Enum.GetNames(typeof(LanguageSetting))));
30+
//TODO: Load previous serialized session data via Load/Save class
31+
Language = SettingsSystemGeneralComponent.LanguageSetting.English;
32+
languageDropdown.SetValueWithoutNotify((int) Language);
33+
}
34+
35+
#region UI CALLBACKS
36+
37+
public void OnChangeLanguage(int languageIndex)
38+
{
39+
Language = (LanguageSetting) languageIndex;
40+
Debug.Log("Language set to: " + Language);
41+
}
42+
43+
#endregion
44+
}

0 commit comments

Comments
 (0)