-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioManager.cs
141 lines (114 loc) · 3.31 KB
/
AudioManager.cs
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
using UnityEngine.Audio;
using System;
using UnityEngine;
using System.Collections;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
public AudioMixerGroup mixerGroup;
public Sound[] sounds;
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.loop = s.loop;
s.source.outputAudioMixerGroup = mixerGroup;
}
}
private void Start()
{
PlayerPrefs.SetInt("MusicEnabled", 1);
PlayerPrefs.SetInt("SoundEnabled", 1);
Play("GameMusic");
}
public void Play(string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
if( !s.source.isPlaying)
s.source.Play();
}
public void PlayOnShot(string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
s.source.PlayOneShot(s.clip);
}
public void PlayWithFadeIn(string sound, float time)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = 0;
StartCoroutine(FadeAudioSource.StartFade(s.source, time, s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f))));
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
if (!s.source.isPlaying)
s.source.Play();
}
public void PlayWithPitch(string sound , float pitch)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
s.source.pitch = pitch;
if (!s.source.isPlaying)
s.source.Play();
}
public void Pause( string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.Pause();
}
public void PauseWithFadeOut(string sound , float time)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
StartCoroutine(FadeAudioSource.StartFade( s.source, time, 0));
StartCoroutine(PauseSound(s, time));
}
IEnumerator PauseSound(Sound s, float delayTime)
{
yield return new WaitForSeconds(delayTime);
// Now do your thing here
s.source.Pause();
}
}