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 ;
9
+
10
+ public class SettingsSystem : MonoBehaviour
11
+ {
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 ;
24
+
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 ; }
29
+
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
+ }
244
+ }
0 commit comments