diff --git a/AudioManager b/AudioManager new file mode 100644 index 0000000..59542aa --- /dev/null +++ b/AudioManager @@ -0,0 +1,76 @@ +using UnityEngine; +using System.Collections; + +public class AudioManager : MonoBehaviour +{ + public AudioSource Play(AudioClip clip, Transform emitter) + { + return Play(clip, emitter, 1f, 1f); + } + + public AudioSource Play(AudioClip clip, Transform emitter, float volume) + { + return Play(clip, emitter, volume, 1f); + } + + /// + /// Plays a sound by creating an empty game object with an AudioSource + /// and attaching it to the given transform (so it moves with the transform). Destroys it after it finished playing. + /// + /// + /// + /// + /// + /// + public AudioSource Play(AudioClip clip, Transform emitter, float volume, float pitch) + { + //Create an empty game object + GameObject go = new GameObject ("Audio: " + clip.name); + go.transform.position = emitter.position; + go.transform.parent = emitter; + + //Create the source + AudioSource source = go.AddComponent(); + source.clip = clip; + source.volume = volume; + source.pitch = pitch; + source.Play (); + Destroy (go, clip.length); + return source; + } + + public AudioSource Play(AudioClip clip, Vector3 point) + { + return Play(clip, point, 1f, 1f); + } + + public AudioSource Play(AudioClip clip, Vector3 point, float volume) + { + return Play(clip, point, volume, 1f); + } + + /// + /// Plays a sound at the given point in space by creating an empty game object with an AudioSource + /// in that place and destroys it after it finished playing. + /// + /// + /// + /// + /// + /// + public AudioSource Play(AudioClip clip, Vector3 point, float volume, float pitch) + { + //Create an empty game object + GameObject go = new GameObject("Audio: " + clip.name); + go.transform.position = point; + + //Create the source + AudioSource source = go.AddComponent(); + source.clip = clip; + source.volume = volume; + source.pitch = pitch; + source.Play(); + Destroy(go, clip.length); + return source; + } +}