-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathAudioManager.cs
More file actions
115 lines (90 loc) · 3.3 KB
/
AudioManager.cs
File metadata and controls
115 lines (90 loc) · 3.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
using UnityEngine;
using System.Collections;
public class AudioManager : MonoBehaviour {
public enum AudioChannel {Master, Sfx, Music};
public float masterVolumePercent { get; private set; }
public float sfxVolumePercent { get; private set; }
public float musicVolumePercent { get; private set; }
AudioSource sfx2DSource;
AudioSource[] musicSources;
int activeMusicSourceIndex;
public static AudioManager instance;
Transform audioListener;
Transform playerT;
SoundLibrary library;
void Awake() {
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
library = GetComponent<SoundLibrary> ();
musicSources = new AudioSource[2];
for (int i = 0; i < 2; i++) {
GameObject newMusicSource = new GameObject ("Music source " + (i + 1));
musicSources [i] = newMusicSource.AddComponent<AudioSource> ();
newMusicSource.transform.parent = transform;
}
GameObject newSfx2Dsource = new GameObject ("2D sfx source");
sfx2DSource = newSfx2Dsource.AddComponent<AudioSource> ();
newSfx2Dsource.transform.parent = transform;
audioListener = FindObjectOfType<AudioListener> ().transform;
if (FindObjectOfType<Player> () != null) {
playerT = FindObjectOfType<Player> ().transform;
}
masterVolumePercent = PlayerPrefs.GetFloat ("master vol", 1);
sfxVolumePercent = PlayerPrefs.GetFloat ("sfx vol", 1);
musicVolumePercent = PlayerPrefs.GetFloat ("music vol", 1);
}
}
void Update() {
if (playerT != null) {
audioListener.position = playerT.position;
}
}
public void SetVolume(float volumePercent, AudioChannel channel) {
switch (channel) {
case AudioChannel.Master:
masterVolumePercent = volumePercent;
break;
case AudioChannel.Sfx:
sfxVolumePercent = volumePercent;
break;
case AudioChannel.Music:
musicVolumePercent = volumePercent;
break;
}
musicSources [0].volume = musicVolumePercent * masterVolumePercent;
musicSources [1].volume = musicVolumePercent * masterVolumePercent;
PlayerPrefs.SetFloat ("master vol", masterVolumePercent);
PlayerPrefs.SetFloat ("sfx vol", sfxVolumePercent);
PlayerPrefs.SetFloat ("music vol", musicVolumePercent);
PlayerPrefs.Save ();
}
public void PlayMusic(AudioClip clip, float fadeDuration = 1) {
activeMusicSourceIndex = 1 - activeMusicSourceIndex;
musicSources [activeMusicSourceIndex].clip = clip;
musicSources [activeMusicSourceIndex].Play ();
StartCoroutine(AnimateMusicCrossfade(fadeDuration));
}
public void PlaySound(AudioClip clip, Vector3 pos) {
if (clip != null) {
AudioSource.PlayClipAtPoint (clip, pos, sfxVolumePercent * masterVolumePercent);
}
}
public void PlaySound(string soundName, Vector3 pos) {
PlaySound (library.GetClipFromName (soundName), pos);
}
public void PlaySound2D(string soundName) {
sfx2DSource.PlayOneShot (library.GetClipFromName (soundName), sfxVolumePercent * masterVolumePercent);
}
IEnumerator AnimateMusicCrossfade(float duration) {
float percent = 0;
while (percent < 1) {
percent += Time.deltaTime * 1 / duration;
musicSources [activeMusicSourceIndex].volume = Mathf.Lerp (0, musicVolumePercent * masterVolumePercent, percent);
musicSources [1-activeMusicSourceIndex].volume = Mathf.Lerp (musicVolumePercent * masterVolumePercent, 0, percent);
yield return null;
}
}
}