-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManagerScript.cs
More file actions
203 lines (182 loc) · 5.31 KB
/
AudioManagerScript.cs
File metadata and controls
203 lines (182 loc) · 5.31 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AudioManagerScript : MonoBehaviour
{
// Unless denoted by a commented out link, TK wrote literally everything here
public AudioClip MainMenuBGM;
public AudioClip GameOverBGM;
public AudioClip VictoryBGM;
public AudioClip GymBGM;
public AudioClip SparringBGM;
public AudioClip MatchBGM;
public AudioClip Rank2MatchBGM;
public AudioClip Rank1MatchBGM;
public AudioClip RankCMatchBGM;
public float volumeMAX;
[System.NonSerialized]
public AudioSource musicSource;
// Start is called before the first frame update
void Start()
{
volumeMAX = 0.5f;
// Get the background music object to use it's AudioSource
GameObject go = GameObject.Find("BGMObject");
musicSource = go.GetComponent<AudioSource>();
musicSource.volume = volumeMAX;
checkSceneBGM();
}
void checkSceneBGM()
{
bool fadeIn = true;
bool loop = true;
AudioClip clippy = null;
float fadeInTime = 0;
Scene scene = SceneManager.GetActiveScene();
if (scene.name == "0MainMenu")
{
loop = true;
fadeIn = true;
fadeInTime = 5.0f;
clippy = MainMenuBGM;
}
if (scene.name == "3Credits")
{
if (PlayerPrefs.GetInt("BeatGame") == 1)
{
Debug.Log("PLAYER BEAT GAME!");
loop = true;
fadeIn = false;
clippy = VictoryBGM;
}
else
{
loop = true;
fadeIn = true;
fadeInTime = 5.0f;
clippy = MainMenuBGM;
}
}
if (scene.name == "1GameOver")
{
loop = false;
fadeIn = false;
clippy = GameOverBGM;
}
if (scene.name == "4GymMenu" || scene.name == "8SpendEXP")
{
loop = true;
fadeIn = true;
fadeInTime = 1.5f;
clippy = GymBGM;
}
if (scene.name == "7RankUp")
{
loop = true;
fadeIn = false;
clippy = VictoryBGM;
}
if (scene.name == "2BeatGame")
{
loop = true;
fadeIn = false;
clippy = VictoryBGM;
}
if (scene.name == "5Match")
{
if (PlayerPrefs.GetInt("Sparring") == 1)
{
// Play the Sparring Music
loop = true;
fadeIn = true;
fadeInTime = 1.5f;
clippy = SparringBGM;
}
else
{
// Play Match Music
loop = true;
fadeIn = true;
fadeInTime = 1.5f;
clippy = MatchBGM;
}
}
checkCurrentBGM(clippy, loop, fadeIn, fadeInTime);
}
void checkCurrentBGM(AudioClip bgm, bool loop, bool fadeIn, float fadeInTime)
{
if (musicSource.isPlaying)
{
if (musicSource.clip != bgm) // change to correct BGM
{
StartBGM(bgm, loop, fadeIn, fadeInTime);
}
else
{
Debug.Log("The correct BGM clip is playing!");
}
}
else // not playing anything
{
StartBGM(bgm, loop, fadeIn, fadeInTime);
}
}
public void StartBGM(AudioClip bgm, bool loop, bool fadeIn, float fadeInTime)
{
StopBGM();
musicSource.loop = loop;
musicSource.clip = bgm;
if (fadeIn)
{
FadeInBGM(fadeInTime);
}
else
{
musicSource.volume = volumeMAX;
musicSource.Play();
}
}
public void FadeOutBGM(float time)
{
if (musicSource == null)
{
Debug.Log("MUSIC SOURCE IS NULL !?!?!?!");
}
StartCoroutine(FadeOut(musicSource, time));
}
public void FadeInBGM(float time)
{
StartCoroutine(FadeIn(musicSource, time));
}
// https://medium.com/@wyattferguson/how-to-fade-out-in-audio-in-unity-8fce422ab1a8
public IEnumerator FadeOut(AudioSource audioSource, float FadeTime)
{
float startVolume = audioSource.volume;
while (audioSource.volume > 0)
{
audioSource.volume -= startVolume * Time.deltaTime / FadeTime;
yield return null;
}
audioSource.volume = 0;
audioSource.Stop();
}
// https://medium.com/@wyattferguson/how-to-fade-out-in-audio-in-unity-8fce422ab1a8
public IEnumerator FadeIn(AudioSource audioSource, float FadeTime)
{
audioSource.Play();
audioSource.volume = 0f;
while (audioSource.volume < volumeMAX)
{
audioSource.volume += Time.deltaTime / FadeTime;
yield return null;
}
}
public void StopBGM()
{
if (musicSource.isPlaying)
{
musicSource.Stop();
}
}
}