-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.cs
More file actions
289 lines (249 loc) · 7.89 KB
/
AudioManager.cs
File metadata and controls
289 lines (249 loc) · 7.89 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
using UnityEngine.Audio;
using System;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Collections;
/* AudioManager.cs
* Max Mitchell
* Last Updated: Oct. 2021
*
* Purpose: implement AudioManager class to connect sound files from the inspector
* to scripting, and allow for other scripts to control pausing, playing,
* and stopping these sounds.
*/
public class AudioManager : MonoBehaviour
{
/* PUBLIC VARIABLES */
public Sound[] sounds; // fill this in the inspector with all your audio clips!
// feel free to add more mixer groups if your game requires more channels!
public AudioMixerGroup mixer;
public AudioMixerGroup musicMixer;
public AudioMixerGroup sfxMixer;
public AudioMixerGroup voicesMixer;
public static AudioManager instance; // class variable; only one AudioManager instance in any scene
public string musicPlaying = "NotMusic";
public bool musicIsPaused;
public Dictionary<string, string> stageMusic = new Dictionary<string, string>();
public bool sceneLoadOverride = false;
/* PRIVATE VARIABLES */
SetVolume setter;
void Awake()
{
setter = FindObjectOfType<SetVolume>();
if(instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
musicPlaying = "NotYet";
DontDestroyOnLoad(gameObject);
Setup();
}
/* Setup()
* <manages the setup of the mixers, and all audio files into the
* AudioManager interface>
*/
private void Setup()
{
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.outputAudioMixerGroup = mixer;
// NAMING CONVENTION:
// beginning of each clips name indicates the type of audio it is
// and which mixer group it will be routed to
if (s.name.Length > 5 && s.name.Substring(0, 5) == "Music")
{
s.source.outputAudioMixerGroup = musicMixer;
}
if (s.name.Length > 3 && s.name.Substring(0, 3) == "SFX")
{
s.source.outputAudioMixerGroup = sfxMixer;
}
if (s.name.Length > 5 && s.name.Substring(0, 5) == "Voice")
{
s.source.outputAudioMixerGroup = voicesMixer;
}
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop ;
}
}
private void Start()
{
// for each scene, must add it's appropriate music to the stageMusic
// TODO: Change this to have the names of your songs and your scenes!
stageMusic.Add("Level", "MusicTitle");
SceneManager.sceneLoaded += SceneLoaded;
SceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
}
/* SceneLoaded(Scene s, LoadSceneMode m)
* <called automatically by Max's script SceneLoader.cs,a and manages the
* changing of scenes while keeping consistent music>
*/
private void SceneLoaded(Scene s, LoadSceneMode m)
{
if (!sceneLoadOverride)
{
string music;
// Find appropriate music for scene
stageMusic.TryGetValue(s.name, out music);
// stop whatever is currently playing unless they are the same song
if (musicPlaying.Substring(0, 5) == "Music" && music != musicPlaying)
{
Stop(musicPlaying);
}
// Now, play music for the stage you have woken up in
if (music != musicPlaying)
{
Play(music, 1);
}
}
sceneLoadOverride = false;
}
/* Play(string name)
* <override for Play that allows playing audio at the default pitch>
*/
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.Log("sound " + name + " not found.");
return;
}
if (name.Length > 5 && name.Substring(0, 5) == "Music")
{
musicPlaying = name;
}
if (!s.source.isPlaying)
{
s.source.Play();
}
}
/* Play(string name, float pitch)
* <plays any sound you have put into the AudioManager. selects sound based
* on string name, and can play at varying pitches given in the float pitch>
*/
public void Play(string name, float pitch)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.Log("sound " + name + " not found.");
return;
}
s.source.pitch = pitch;
if(name.Length > 5 && name.Substring(0, 5) == "Music")
{
musicPlaying = name;
}
s.source.Play();
}
/* PlayFromSource(string name, float pitch, float volume, AudioSource audiosource)
* <functions the same as Play, but allows playing from a localized AudioSource instead of the
* general MainCamera's AudioSource. also takes a float for variable volume>
*/
public void PlayFromSource(string name, float pitch, float volume, AudioSource audiosource)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.Log("sound " + name + " not found.");
return;
}
if (name.Length > 5 && name.Substring(0, 5) == "Music")
{
musicPlaying = name;
}
if (!s.source.isPlaying)
{
audiosource.clip = s.source.clip;
audiosource.outputAudioMixerGroup = s.source.outputAudioMixerGroup;
audiosource.volume = s.source.volume;
audiosource.pitch = pitch;
audiosource.loop = s.source.loop;
audiosource.spatialBlend = 1f;
audiosource.Play();
}
}
/* Stop(string name)
* <stops the sound given by the string name>
*/
public void Stop(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s != null)
{
s.source.Stop();
}
}
/* Pause(string name)
* <pauses the sound given by the string name. this is different from
* stopping, since pause allows you to resume the sound clip where
* you paused it from>
*/
public void Pause(string name)
{
musicIsPaused = true;
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Pause();
}
/* PauseAll()
* <pauses all sounds in the scene>
*/
public void PauseAll()
{
musicIsPaused = true;
foreach (Sound s in sounds)
{
s.source.Pause();
}
// also pause from every audiosource in game
foreach (AudioSource AS in FindObjectsOfType<AudioSource>())
{
AS.Pause();
}
}
/* Unpause(string name)
* <unpauses the sound given by string name>
*/
public void UnPause(string name)
{
musicIsPaused = false;
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.UnPause();
}
/* UnPauseAll()
* <unpauses all sounds in the scene>
*/
public void UnPauseAll()
{
musicIsPaused = false;
foreach (Sound s in sounds)
{
s.source.Pause();
s.source.UnPause();
}
// also unpause from every audiosource in game
foreach (AudioSource AS in FindObjectsOfType<AudioSource>())
{
AS.Pause();
AS.UnPause();
}
}
/* UnPauseMusic()
* <unpauses just the music that is currently playing>
*/
public void UnPauseMusic()
{
UnPause(musicPlaying);
}
}