Created
May 9, 2016 01:22
-
-
Save eddietree/6cfecbd8bb1e6f3e1e035149c5991443 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public struct AudioPlayerParams | |
{ | |
public float minDistance; | |
public float maxDistance; | |
public float volume; | |
public bool dopplerEnabled; | |
public bool autoPlay; // auto play when it starts? | |
public bool loop; | |
public bool autoKill; // destroy itself when not playing? | |
public static AudioPlayerParams GetDefaultParams() | |
{ | |
AudioPlayerParams audioParams = new AudioPlayerParams(); | |
audioParams.minDistance = 0.4f; | |
audioParams.maxDistance = 5.0f; | |
audioParams.volume = 1.0f; | |
audioParams.dopplerEnabled = false; | |
audioParams.autoPlay = true; | |
audioParams.loop = false; | |
audioParams.autoKill = true; | |
return audioParams; | |
} | |
public static AudioPlayerParams GetDefaultLoopParams() | |
{ | |
AudioPlayerParams audioParams = AudioPlayerParams.GetDefaultParams(); | |
audioParams.loop = true; | |
audioParams.autoKill = false; | |
return audioParams; | |
} | |
} | |
public class AudioPlayer : MonoBehaviour | |
{ | |
public AudioPlayerParams audioParams; | |
public AudioSource audioSource = null; | |
public AudioClip audioClip = null; | |
public TBE.Components.TBE_SourceControl tbeSource = null; | |
public static AudioPlayer PlayOneShot(string path, Vector3 pos) | |
{ | |
return PlayOneShot(path, pos, AudioPlayerParams.GetDefaultParams()); | |
} | |
// this will attach the audio to the parentObj | |
public static AudioPlayer PlayOneShot(string path, GameObject parentObj) | |
{ | |
return PlayOneShot(path, parentObj, AudioPlayerParams.GetDefaultParams()); | |
} | |
public static AudioPlayer PlayOneShot(string path, Vector3 pos, AudioPlayerParams audioParams ) | |
{ | |
// out of range | |
if (!IsPtInCameraRange(pos, audioParams.maxDistance)) | |
return null; | |
var gameObj = new GameObject(); | |
gameObj.transform.position = pos; | |
gameObj.name = "AudioPlayer (PlayOneShot)"; | |
var audioPlayer = gameObj.AddComponent<AudioPlayer>(); | |
audioPlayer.InitFile(path, audioParams); | |
return audioPlayer; | |
} | |
public static AudioPlayer PlayOneShot(string path, GameObject parentObj, AudioPlayerParams audioParams) | |
{ | |
// out of range | |
if (!IsPtInCameraRange(parentObj.transform.position, audioParams.maxDistance)) | |
return null; | |
var gameObj = new GameObject(); | |
gameObj.name = "AudioPlayer (PlayOneShot)"; | |
gameObj.transform.SetParent(parentObj.transform); | |
var audioPlayer = gameObj.AddComponent<AudioPlayer>(); | |
audioPlayer.InitFile(path, audioParams); | |
return audioPlayer; | |
} | |
public static AudioPlayer PlayOneShotRandomPitch(string path, Vector3 pos, float pitchRange = 0.15f) | |
{ | |
var result = PlayOneShot(path, pos); | |
if (result) | |
result.audioSource.pitch += Random.Range(-1.0f, 1.0f) * pitchRange; | |
return result; | |
} | |
public static AudioPlayer PlayOneShotRandom(string path, Vector3 pos, int numClips, float pitchRange = 0.0f) | |
{ | |
var pathRandom = path + UnityEngine.Random.Range(1, numClips + 1); | |
return AudioPlayer.PlayOneShotRandomPitch(pathRandom, pos, pitchRange); | |
} | |
public static AudioPlayer PlayErrorSound(Vector3 pos) | |
{ | |
return PlayOneShot("UI/error", pos); | |
} | |
// loops are always created always creates, never | |
public static AudioPlayer CreateLoop(string path) | |
{ | |
return AudioPlayer.CreateLoop(path, AudioPlayerParams.GetDefaultLoopParams()); | |
} | |
public static AudioPlayer CreateLoop(string path, AudioPlayerParams audioParams) | |
{ | |
var gameObj = new GameObject(); | |
gameObj.name = "AudioPlayer (Loop)"; | |
var audioPlayer = gameObj.AddComponent<AudioPlayer>(); | |
audioPlayer.InitFile(path, audioParams); | |
return audioPlayer; | |
} | |
private static bool IsPtInCameraRange( Vector3 audioPos, float audioRange ) | |
{ | |
var camPos = Camera.main.transform.position; | |
return (camPos - audioPos).sqrMagnitude < (audioRange * audioRange); | |
} | |
void Start () | |
{} | |
void Update () | |
{ | |
// will destroy if not playing | |
if (audioParams.autoKill && (audioSource == null || !audioSource.isPlaying) ) | |
{ | |
Kill(); | |
} | |
} | |
private void InitFile( string path, AudioPlayerParams initParams ) | |
{ | |
// tbe source | |
tbeSource = gameObject.AddComponent<TBE.Components.TBE_SourceControl>(); | |
//if (!tbeSource) audioSource = gameObject.AddComponent<AudioSource>(); | |
// create audio clip | |
var fullPath = string.Format("Audio/{0}", path); | |
audioClip = Resources.Load<AudioClip>(fullPath); | |
// create audio src | |
audioSource = gameObject.GetComponent<AudioSource>(); | |
audioSource.clip = audioClip; | |
InitWithParams(initParams); | |
if (initParams.autoPlay) | |
{ | |
Play(); | |
} | |
} | |
public void InitWithParams(AudioPlayerParams initParams) | |
{ | |
audioParams = initParams; | |
if (tbeSource) | |
{ | |
tbeSource.maxDistanceMute = true; | |
tbeSource.minimumDistance = audioParams.minDistance; | |
tbeSource.maximumDistance = audioParams.maxDistance; | |
tbeSource.dopplerToggle = audioParams.dopplerEnabled; | |
} | |
audioSource.playOnAwake = false; | |
audioSource.volume = audioParams.volume; | |
audioSource.loop = audioParams.loop; | |
audioSource.minDistance = audioParams.minDistance; | |
audioSource.maxDistance = audioParams.maxDistance; | |
} | |
public void Play() | |
{ | |
if (!audioSource) | |
return; | |
audioSource.Play(); | |
} | |
public void Stop() | |
{ | |
if (!audioSource) | |
return; | |
audioSource.Stop(); | |
} | |
public void SetVolumeLerp(float goalVol, float lerpCoeff) | |
{ | |
audioSource.volume = Mathf.Lerp(audioSource.volume, goalVol, lerpCoeff); | |
} | |
public void SetVolume(float vol) | |
{ | |
audioSource.volume = vol; | |
} | |
public float GetVolume() | |
{ | |
return audioSource.volume; | |
} | |
public void SetPitchLerp(float goalPitch, float lerpCoeff) | |
{ | |
audioSource.pitch = Mathf.Lerp(audioSource.pitch, goalPitch, lerpCoeff); | |
} | |
public void SetPitch(float pitch) | |
{ | |
audioSource.pitch = pitch; | |
} | |
public float GetPitch() | |
{ | |
return audioSource.pitch; | |
} | |
public void Kill() | |
{ | |
Destroy(gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment