-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoundManager.cs
More file actions
107 lines (91 loc) · 2.51 KB
/
SoundManager.cs
File metadata and controls
107 lines (91 loc) · 2.51 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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoundManager : Singleton<SoundManager>
{
private Dictionary<int, AudioClip> soundMap;
private List<AudioSource> _audioSource;
//Change the number of audio sources as needed
private int _audioSourceCount = 10;
//Music
public AudioClip testSound { get; private set; }
public void playSound(AudioClip sound)
{
foreach (AudioSource AS in _audioSource)
{
if (!AS.isPlaying)
{
AS.PlayOneShot(sound);
break;
}
}
}
public void playSound(AudioClip sound, int index)
{
if (index > _audioSourceCount)
{
index = _audioSourceCount;
}
_audioSource[index - 1].PlayOneShot(sound);
}
public void playSound(AudioClip sound, int index, float volume)
{
if (index > _audioSourceCount)
{
index = _audioSourceCount;
}
_audioSource[index - 1].PlayOneShot(sound, volume);
}
public void stopSound(int index)
{
if (index > _audioSourceCount)
{
index = _audioSourceCount;
}
StartCoroutine(fadeSound(index, 1));
}
public void stopAllSounds()
{
foreach (AudioSource AS in _audioSource)
{
AS.Stop();
}
}
// Use this for initialization
void Awake()
{
_audioSource = new List<AudioSource>();
soundMap = new Dictionary<int, AudioClip>();
for (int i = 0; i < _audioSourceCount; i++)
{
AudioSource temp = gameObject.AddComponent<AudioSource>();
_audioSource.Add(temp);
}
//Loads a file from Resources/Sounds folder
testSound = _loadSoundClip("TestSound", 0);
}
private AudioClip _loadSoundClip(string filename, int i)
{
AudioClip clip = Resources.Load("Sounds/" + filename) as AudioClip;
soundMap.Add(i, clip);
return clip;
}
public AudioClip getSound(int idx)
{
Debug.Log(soundMap.ContainsKey(idx));
ICollection coll = soundMap.Keys;
return soundMap[idx];
}
IEnumerator fadeSound(int index, float time)
{
AudioSource AS = _audioSource[index - 1];
float delta = AS.volume / time;
while (AS.volume > 0.05f)
{
AS.volume -= delta * Time.deltaTime;
yield return null;
}
AS.Stop();
AS.volume = 1;
}
}