-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioManager.cs
61 lines (52 loc) · 1.5 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
using UnityEngine;
using System.Collections;
public class AudioManager : MonoBehaviour {
// public static AudioManager Init(){
// GameObject gobj = new GameObject();
// gobj.name = "AudioManager";
// return gobj.AddComponent<AudioManager>();
// DontDestroyOnLoad(gobj);
// }
// public static AudioManager instance;
public static void PlaySound(string path, bool loop = false, string name = "AudioSource"){
AudioClip ac = Resources.Load(path) as AudioClip;
PlaySound(ac, loop, name);
}
public static GameObject PlaySound(AudioClip ac, bool loop = false, string name = "AudioSource"){
if(ac == null){
Debug.LogWarning("Play Sound AC Is Null");
return null;
}
GameObject gobjSound = new GameObject();
gobjSound.name = name;
AudioSource audioS = gobjSound.AddComponent<AudioSource>();
audioS.clip = ac;
audioS.loop = loop;
if((loop && GameManager.enableMusic) || (!loop && GameManager.enableSound)){
audioS.Play();
}
if(!loop){
Destroy (gobjSound, ac.length);
}
return gobjSound;
}
public static void StopSound(string name){
GameObject gobjSound = GameObject.Find(name);
if(gobjSound != null){
AudioSource audio = gobjSound.GetComponent<AudioSource>();
audio.Stop();
}
}
public static bool ContineSound(string name){
bool success = false;
GameObject gobjSound = GameObject.Find(name);
if(gobjSound != null){
AudioSource audio = gobjSound.GetComponent<AudioSource>();
if(!audio.isPlaying){
audio.Play();
}
success = true;
}
return success;
}
}