Skip to content

Create AudioManager #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions AudioManager
Original file line number Diff line number Diff line change
@@ -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);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="clip"></param>
/// <param name="emitter"></param>
/// <param name="volume"></param>
/// <param name="pitch"></param>
/// <returns></returns>
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<AudioSource>();
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);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="clip"></param>
/// <param name="point"></param>
/// <param name="volume"></param>
/// <param name="pitch"></param>
/// <returns></returns>
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<AudioSource>();
source.clip = clip;
source.volume = volume;
source.pitch = pitch;
source.Play();
Destroy(go, clip.length);
return source;
}
}