Last active
May 14, 2018 09:53
-
-
Save buijldert/0d6870e4dc026e7b8deb33ebb5d83175 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System.Collections; | |
using UnityEngine; | |
using DG.Tweening; | |
namespace Audio | |
{ | |
public class SFXManager : MonoBehaviour | |
{ | |
//Instance of this script. | |
private static SFXManager s_Instance = null; | |
/// <summary> | |
/// Instantiates a new SoundManager if one cannot be found. | |
/// </summary> | |
public static SFXManager instance | |
{ | |
get | |
{ | |
if (s_Instance == null) | |
{ | |
s_Instance = FindObjectOfType(typeof(SFXManager)) as SFXManager; | |
} | |
if (s_Instance == null) | |
{ | |
GameObject obj = new GameObject("SoundManager"); | |
s_Instance = obj.AddComponent(typeof(SFXManager)) as SFXManager; | |
} | |
return s_Instance; | |
} | |
set { } | |
} | |
/// <summary> | |
/// Removes the instance of the SoundManager | |
/// </summary> | |
void OnApplicationQuit() | |
{ | |
s_Instance = null; | |
} | |
/// <summary> | |
/// Loads the sound clips from the Resources/Audio folder so they can be used. | |
/// Also makes sure the sound manager persists through every scene. | |
/// </summary> | |
private void Awake() | |
{ | |
if (s_Instance != null && s_Instance != this) | |
{ | |
Destroy(gameObject); | |
} | |
s_Instance = this; | |
DontDestroyOnLoad(gameObject); | |
} | |
/// <summary> | |
/// Plays the given at default volume and pitch if no volume and pitch are given. | |
/// </summary> | |
/// <param name="clipToPlay">The clip that will be played.</param> | |
/// <param name="volume">The volume at which the clip will be played.</param> | |
/// <param name="pitch">The pitch at which the clip will be played.</param> | |
public void PlaySound(AudioClip clipToPlay, float volume = 1f, float pitch = 1f) | |
{ | |
StartCoroutine(SimultaneousSound(clipToPlay, volume, pitch)); | |
} | |
/// <summary> | |
/// Plays the given at default volume and pitch if no volume and pitch are given. | |
/// Plays simultaneously with other sounds. | |
/// </summary> | |
/// <param name="clipToPlay">The clip that will be played.</param> | |
/// <param name="volume">The volume at which the clip will be played.</param> | |
/// <param name="pitch">The pitch at which the clip will be played.</param> | |
private IEnumerator SimultaneousSound(AudioClip clipToPlay, float volume = 1f, float pitch = 1f) | |
{ | |
AudioSource tempAS = gameObject.AddComponent<AudioSource>(); | |
tempAS.clip = clipToPlay; | |
tempAS.volume = volume; | |
tempAS.pitch = pitch; | |
tempAS.Play(); | |
yield return new WaitForSeconds(clipToPlay.length); | |
Destroy(tempAS); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment