-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudioManager.cs
More file actions
452 lines (375 loc) · 13.3 KB
/
AudioManager.cs
File metadata and controls
452 lines (375 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
using UnityEngine;
using UnityEngine.Audio;
using System.Collections.Generic;
using System;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance { get; private set; }
[Header("Audio Sources")]
[SerializeField] private AudioSource musicSource;
[SerializeField] private AudioSource sfxSource;
[SerializeField] private AudioSource ambienceSource;
[SerializeField] private AudioSource uiSource;
[Header("Audio Mixers")]
[SerializeField] private AudioMixer audioMixer;
[Header("Sound Collections")]
[SerializeField] private Sound[] musicTracks;
[SerializeField] private Sound[] sfxSounds;
[SerializeField] private Sound[] ambienceSounds;
[SerializeField] private Sound[] uiSounds;
// Dictionary for quick lookup of sounds by name
private Dictionary<string, Sound> soundDictionary = new Dictionary<string, Sound>();
// Track currently playing sounds
private string currentMusicTrack;
private string currentAmbience;
// Volume settings
private float masterVolume = 1f;
private float musicVolume = 1f;
private float sfxVolume = 1f;
private float ambienceVolume = 1f;
private float uiVolume = 1f;
// For fade effects
private Coroutine musicFadeCoroutine;
private Coroutine ambienceFadeCoroutine;
private void Awake()
{
// Singleton pattern
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
// Initialize sound dictionary
RegisterSounds(musicTracks);
RegisterSounds(sfxSounds);
RegisterSounds(ambienceSounds);
RegisterSounds(uiSounds);
// Set up audio sources if not assigned
if (musicSource == null)
{
musicSource = gameObject.AddComponent<AudioSource>();
musicSource.loop = true;
musicSource.playOnAwake = false;
}
if (sfxSource == null)
{
sfxSource = gameObject.AddComponent<AudioSource>();
sfxSource.loop = false;
sfxSource.playOnAwake = false;
}
if (ambienceSource == null)
{
ambienceSource = gameObject.AddComponent<AudioSource>();
ambienceSource.loop = true;
ambienceSource.playOnAwake = false;
}
if (uiSource == null)
{
uiSource = gameObject.AddComponent<AudioSource>();
uiSource.loop = false;
uiSource.playOnAwake = false;
}
// Load saved volumes
LoadVolumeSettings();
ApplyVolumeSettings();
}
private void RegisterSounds(Sound[] sounds)
{
foreach (Sound sound in sounds)
{
if (!soundDictionary.ContainsKey(sound.name))
{
soundDictionary.Add(sound.name, sound);
}
else
{
Debug.LogWarning("Duplicate sound name found: " + sound.name);
}
}
}
#region Music Functions
public void PlayMusic(string name, float fadeInDuration = 1f)
{
if (name == currentMusicTrack && musicSource.isPlaying)
return;
if (!soundDictionary.ContainsKey(name))
{
Debug.LogWarning("Music track not found: " + name);
return;
}
Sound sound = soundDictionary[name];
if (fadeInDuration > 0 && musicSource.isPlaying)
{
StartCoroutine(FadeMusicTrack(sound, fadeInDuration));
}
else
{
musicSource.clip = sound.clip;
musicSource.volume = sound.volume * musicVolume;
musicSource.pitch = sound.pitch;
musicSource.Play();
}
currentMusicTrack = name;
}
private System.Collections.IEnumerator FadeMusicTrack(Sound newTrack, float fadeDuration)
{
float startVolume = musicSource.volume;
float timer = 0;
// Fade out current track
while (timer < fadeDuration / 2)
{
timer += Time.deltaTime;
musicSource.volume = Mathf.Lerp(startVolume, 0, timer / (fadeDuration / 2));
yield return null;
}
// Switch to new track
musicSource.clip = newTrack.clip;
musicSource.pitch = newTrack.pitch;
musicSource.Play();
// Fade in new track
timer = 0;
while (timer < fadeDuration / 2)
{
timer += Time.deltaTime;
musicSource.volume = Mathf.Lerp(0, newTrack.volume * musicVolume, timer / (fadeDuration / 2));
yield return null;
}
musicSource.volume = newTrack.volume * musicVolume;
}
public void StopMusic(float fadeOutDuration = 1f)
{
if (!musicSource.isPlaying)
return;
if (fadeOutDuration > 0)
{
StartCoroutine(FadeOut(musicSource, fadeOutDuration));
}
else
{
musicSource.Stop();
}
currentMusicTrack = null;
}
private System.Collections.IEnumerator FadeOut(AudioSource source, float fadeDuration)
{
float startVolume = source.volume;
float timer = 0;
while (timer < fadeDuration)
{
timer += Time.deltaTime;
source.volume = Mathf.Lerp(startVolume, 0, timer / fadeDuration);
yield return null;
}
source.Stop();
source.volume = startVolume;
}
#endregion
#region SFX Functions
public void PlaySFX(string name)
{
if (!soundDictionary.ContainsKey(name))
{
Debug.LogWarning("SFX not found: " + name);
return;
}
Sound sound = soundDictionary[name];
sfxSource.PlayOneShot(sound.clip, sound.volume * sfxVolume);
}
public void PlaySFXAtPosition(string name, Vector3 position, float minDistance = 1f, float maxDistance = 50f)
{
if (!soundDictionary.ContainsKey(name))
{
Debug.LogWarning("SFX not found: " + name);
return;
}
Sound sound = soundDictionary[name];
// Create temporary audio source at the specified position
GameObject tempAudio = new GameObject("TempAudio");
tempAudio.transform.position = position;
AudioSource tempSource = tempAudio.AddComponent<AudioSource>();
tempSource.clip = sound.clip;
tempSource.volume = sound.volume * sfxVolume;
tempSource.pitch = sound.pitch;
tempSource.spatialBlend = 1f; // 3D sound
tempSource.minDistance = minDistance;
tempSource.maxDistance = maxDistance;
tempSource.rolloffMode = AudioRolloffMode.Linear;
tempSource.Play();
// Destroy the GameObject after the sound is done playing
float clipLength = sound.clip.length;
Destroy(tempAudio, clipLength + 0.1f);
}
#endregion
#region Ambience Functions
public void PlayAmbience(string name, float fadeInDuration = 2f)
{
if (name == currentAmbience && ambienceSource.isPlaying)
return;
if (!soundDictionary.ContainsKey(name))
{
Debug.LogWarning("Ambience not found: " + name);
return;
}
Sound sound = soundDictionary[name];
if (fadeInDuration > 0 && ambienceSource.isPlaying)
{
StartCoroutine(FadeAmbienceTrack(sound, fadeInDuration));
}
else
{
ambienceSource.clip = sound.clip;
ambienceSource.volume = sound.volume * ambienceVolume;
ambienceSource.pitch = sound.pitch;
ambienceSource.Play();
}
currentAmbience = name;
}
private System.Collections.IEnumerator FadeAmbienceTrack(Sound newTrack, float fadeDuration)
{
float startVolume = ambienceSource.volume;
float timer = 0;
// Fade out current track
while (timer < fadeDuration / 2)
{
timer += Time.deltaTime;
ambienceSource.volume = Mathf.Lerp(startVolume, 0, timer / (fadeDuration / 2));
yield return null;
}
// Switch to new track
ambienceSource.clip = newTrack.clip;
ambienceSource.pitch = newTrack.pitch;
ambienceSource.Play();
// Fade in new track
timer = 0;
while (timer < fadeDuration / 2)
{
timer += Time.deltaTime;
ambienceSource.volume = Mathf.Lerp(0, newTrack.volume * ambienceVolume, timer / (fadeDuration / 2));
yield return null;
}
ambienceSource.volume = newTrack.volume * ambienceVolume;
}
public void StopAmbience(float fadeOutDuration = 2f)
{
if (!ambienceSource.isPlaying)
return;
if (fadeOutDuration > 0)
{
StartCoroutine(FadeOut(ambienceSource, fadeOutDuration));
}
else
{
ambienceSource.Stop();
}
currentAmbience = null;
}
#endregion
#region UI Sound Functions
public void PlayUISound(string name)
{
if (!soundDictionary.ContainsKey(name))
{
Debug.LogWarning("UI sound not found: " + name);
return;
}
Sound sound = soundDictionary[name];
uiSource.PlayOneShot(sound.clip, sound.volume * uiVolume);
}
#endregion
#region Volume Control
public void SetMasterVolume(float volume)
{
masterVolume = Mathf.Clamp01(volume);
ApplyVolumeSettings();
SaveVolumeSettings();
}
public void SetMusicVolume(float volume)
{
musicVolume = Mathf.Clamp01(volume);
ApplyVolumeSettings();
SaveVolumeSettings();
}
public void SetSFXVolume(float volume)
{
sfxVolume = Mathf.Clamp01(volume);
ApplyVolumeSettings();
SaveVolumeSettings();
}
public void SetAmbienceVolume(float volume)
{
ambienceVolume = Mathf.Clamp01(volume);
ApplyVolumeSettings();
SaveVolumeSettings();
}
public void SetUIVolume(float volume)
{
uiVolume = Mathf.Clamp01(volume);
ApplyVolumeSettings();
SaveVolumeSettings();
}
public float GetMasterVolume() => masterVolume;
public float GetMusicVolume() => musicVolume;
public float GetSFXVolume() => sfxVolume;
public float GetAmbienceVolume() => ambienceVolume;
public float GetUIVolume() => uiVolume;
private void ApplyVolumeSettings()
{
// Apply volumes to audio mixer if available
if (audioMixer != null)
{
// Convert linear volume to logarithmic for better control
float masterVolumeDB = masterVolume > 0.001f ? 20f * Mathf.Log10(masterVolume) : -80f;
float musicVolumeDB = musicVolume > 0.001f ? 20f * Mathf.Log10(musicVolume) : -80f;
float sfxVolumeDB = sfxVolume > 0.001f ? 20f * Mathf.Log10(sfxVolume) : -80f;
float ambienceVolumeDB = ambienceVolume > 0.001f ? 20f * Mathf.Log10(ambienceVolume) : -80f;
float uiVolumeDB = uiVolume > 0.001f ? 20f * Mathf.Log10(uiVolume) : -80f;
audioMixer.SetFloat("MasterVolume", masterVolumeDB);
audioMixer.SetFloat("MusicVolume", musicVolumeDB);
audioMixer.SetFloat("SFXVolume", sfxVolumeDB);
audioMixer.SetFloat("AmbienceVolume", ambienceVolumeDB);
audioMixer.SetFloat("UIVolume", uiVolumeDB);
}
// Apply directly to sources if mixer not available
if (musicSource != null && musicSource.clip != null && soundDictionary.ContainsKey(currentMusicTrack))
{
musicSource.volume = soundDictionary[currentMusicTrack].volume * musicVolume * masterVolume;
}
if (ambienceSource != null && ambienceSource.clip != null && soundDictionary.ContainsKey(currentAmbience))
{
ambienceSource.volume = soundDictionary[currentAmbience].volume * ambienceVolume * masterVolume;
}
}
private void SaveVolumeSettings()
{
PlayerPrefs.SetFloat("MasterVolume", masterVolume);
PlayerPrefs.SetFloat("MusicVolume", musicVolume);
PlayerPrefs.SetFloat("SFXVolume", sfxVolume);
PlayerPrefs.SetFloat("AmbienceVolume", ambienceVolume);
PlayerPrefs.SetFloat("UIVolume", uiVolume);
PlayerPrefs.Save();
}
private void LoadVolumeSettings()
{
masterVolume = PlayerPrefs.GetFloat("MasterVolume", 1f);
musicVolume = PlayerPrefs.GetFloat("MusicVolume", 1f);
sfxVolume = PlayerPrefs.GetFloat("SFXVolume", 1f);
ambienceVolume = PlayerPrefs.GetFloat("AmbienceVolume", 1f);
uiVolume = PlayerPrefs.GetFloat("UIVolume", 1f);
}
#endregion
}
[System.Serializable]
public class Sound
{
public string name;
public AudioClip clip;
[Range(0f, 1f)]
public float volume = 1f;
[Range(0.1f, 3f)]
public float pitch = 1f;
}