Last active
March 29, 2018 16:39
-
-
Save ihsanberahim/cb95eda84d5b1000c6d1 to your computer and use it in GitHub Desktop.
Unity SoundsManager
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class GameSystem : MonoBehaviour { | |
void Start() | |
{ | |
//Always call the init function first to use SoundsManager component | |
SoundsManager.init(); | |
SoundsManager.instance.PlaySound (SoundsManager.AUDIO_INGAME, new Dictionary<string, object>{ | |
{SoundsManager.AUDIO_OPT_FADEIN,true}, | |
{SoundsManager.AUDIO_OPT_LOOP,true}, | |
{SoundsManager.AUDIO_OPT_VOLUME,0.5} | |
}); | |
} | |
void OnDestroy() | |
{ | |
SoundsManager.instance.FadeOutSound (SoundsManager.AUDIO_INGAME); | |
} | |
} | |
/* | |
CREDIT | |
ihsanberahim.com | |
fiction-labs.com | |
pkmedia.com.my | |
*/ |
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
/* | |
GUIDES: | |
1. Remove All Audio Listener from you "Main Camera" in all scenes. | |
2. call "SoundsManager.init()" in all you main scene script at Start() | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
[RequireComponent(typeof(AudioListener))] | |
public class SoundsManager : MonoBehaviour { | |
public static SoundsManager instance; | |
public const string AUDIO_INMENU = "inmenu"; | |
public const string AUDIO_INGAME = "ingame"; | |
public const string AUDIO_WRONGANSWER = "wronganswer"; | |
public const string AUDIO_RIGHTANSWER = "rightanswer"; | |
public const string AUDIO_POWERUP_FEEZETIME = "freezetime"; | |
public const string AUDIO_POWERUP_ROWSOLVE = "rowsolve"; | |
public const string AUDIO_POWERUP_MEGASOLVE = "megasolve"; | |
public const string AUDIO_ALMOST_GAMEOVER = "almost_gameover"; | |
public const string AUDIO_OPT_FADEIN = "FadeIn"; | |
public const string AUDIO_OPT_FADEOUT = "FadeOut"; | |
public const string AUDIO_OPT_LOOP = "loop"; | |
public const string AUDIO_OPT_VOLUME = "volume"; | |
public const string AUDIO_OPT_DELAY = "delay"; | |
public const string AUDIO_OPT_PITCH = "pitch"; | |
public const string AUDIO_OPT_PITCH_TIMEOUT = "pitch_timeout"; | |
public const string AUDIO_OPT_TIME = "time"; | |
public static Dictionary<string, AudioSource> playlist; | |
public static Dictionary<string, string> audioEvent; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
#pragma warning restore 0067 | |
private void Awake() | |
{ | |
// Set the GameObject name to the class name for easy access from native plugin | |
gameObject.name = GetType().ToString(); | |
DontDestroyOnLoad (gameObject); | |
} | |
public static void init() | |
{ | |
if( FindObjectOfType(typeof(SoundsManager)) == null ) | |
{ | |
playlist = new Dictionary<string, AudioSource> (); | |
audioEvent = new Dictionary<string, string> (); | |
GameObject newGSM = new GameObject(); | |
newGSM.AddComponent<SoundsManager>(); | |
instance = (SoundsManager)newGSM.GetComponent("SoundsManager"); | |
} | |
else | |
{ | |
instance = (SoundsManager)FindObjectOfType(typeof(SoundsManager)); | |
} | |
} | |
public static void printAudioEvents() | |
{ | |
string all = ""; | |
foreach(KeyValuePair<string, string> theevent in SoundsManager.audioEvent) | |
{ | |
all += theevent.Key+" ... "+theevent.Value+", "; | |
} | |
Debug.Log ("PrintAudioEvents: "+all); | |
} | |
public static void printPlaylist() | |
{ | |
string all = ""; | |
foreach(KeyValuePair<string, AudioSource> play in SoundsManager.playlist) | |
{ | |
all += play.Value.clip.name+", "; | |
} | |
Debug.Log ("PrintPlaylist: "+all); | |
} | |
public void PlaySound(string sourceName) | |
{ | |
PlaySound (sourceName, new Dictionary<string, object> (){}); | |
} | |
public void PlaySound(string sourceName, Dictionary<string, object> options) | |
{ | |
AudioSource soundPlayer; | |
AudioClip requestedSound; | |
object option; | |
//Options: Fade In | |
bool isFadeIn = false; | |
if(options.ContainsKey(AUDIO_OPT_FADEIN)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_FADEIN,out option); | |
isFadeIn = (bool) option; | |
if(isFadeIn) | |
FadeInSound (sourceName); | |
} | |
//Options: Loop | |
bool isLoop = false; | |
if(options.ContainsKey(AUDIO_OPT_LOOP)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_LOOP,out option); | |
isLoop = (bool) option; | |
} | |
//Options: volume | |
float volume = 1; | |
if(options.ContainsKey(AUDIO_OPT_VOLUME)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_VOLUME,out option); | |
float.TryParse(option.ToString(),out volume); | |
} | |
//Options: Delay | |
float delay = 0; | |
if(options.ContainsKey(AUDIO_OPT_DELAY)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_DELAY,out option); | |
delay = float.Parse (option.ToString()); | |
} | |
if (!playlist.ContainsKey(sourceName)) | |
{ | |
soundPlayer = gameObject.AddComponent<AudioSource>(); | |
requestedSound = Resources.Load ("Sounds/"+sourceName,typeof(AudioClip)) as AudioClip; | |
if(requestedSound.GetType()==typeof(AudioClip)) | |
{ | |
soundPlayer.clip = requestedSound; | |
soundPlayer.loop = isLoop; | |
soundPlayer.volume = volume; | |
playlist.Add(sourceName,soundPlayer); | |
}else | |
Debug.Log ("Sounds ... "+ sourceName + " not found"); | |
} | |
playlist.TryGetValue (sourceName,out soundPlayer); | |
//Start | |
if(soundPlayer!=null) | |
{ | |
if(delay > 0) | |
soundPlayer.PlayDelayed (delay); | |
else | |
soundPlayer.Play (); | |
} | |
} | |
public void FadeOutSound(string sourceName) | |
{ | |
AudioSource soundPlayer; | |
SoundsManager.playlist.TryGetValue (sourceName,out soundPlayer); | |
if (soundPlayer!=null) | |
{ | |
iTween.AudioTo(gameObject,iTween.Hash ( | |
"audiosource",soundPlayer, | |
"volume",0, | |
"time",0.05F)); | |
audioEvent.Remove (sourceName); | |
audioEvent.Add (sourceName,AUDIO_OPT_FADEOUT); | |
}else | |
{ | |
Debug.Log ("FadeOutSound ... "+sourceName+" not found"); | |
} | |
SoundsManager.printAudioEvents (); | |
} | |
public void PitchSound(string sourceName) | |
{ | |
PitchSound(sourceName, new Dictionary<string, object>(){ | |
{AUDIO_OPT_PITCH,0.5}, | |
{AUDIO_OPT_PITCH_TIMEOUT,3} | |
}); | |
} | |
public void PitchSound(string sourceName, Dictionary<string, object> options) | |
{ | |
AudioSource soundPlayer; | |
object option; | |
SoundsManager.playlist.TryGetValue (sourceName,out soundPlayer); | |
//Option: Pitch Value | |
float pitch = 1; | |
if(options.ContainsKey(AUDIO_OPT_PITCH)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_PITCH,out option); | |
float.TryParse (option.ToString(),out pitch); | |
} | |
//Option: Pitch Time | |
float time = 1; | |
if(options.ContainsKey(AUDIO_OPT_TIME)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_TIME,out option); | |
float.TryParse (option.ToString(),out time); | |
} | |
//Option: Pitch Timeout | |
int pitchTimeout = 1; | |
if(options.ContainsKey(AUDIO_OPT_PITCH_TIMEOUT)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_PITCH_TIMEOUT,out option); | |
int.TryParse (option.ToString(),out pitchTimeout); | |
} | |
//Start | |
if(soundPlayer!=null) | |
{ | |
iTween.AudioTo(gameObject,iTween.Hash ( | |
"audiosource",soundPlayer, | |
"pitch",pitch, | |
"time",time)); | |
StartCoroutine(PitchTimeout(soundPlayer,pitchTimeout)); | |
} | |
} | |
public void FadeInSound(string sourceName) | |
{ | |
FadeInSound(sourceName, new Dictionary<string, object>(){}); | |
} | |
public void FadeInSound(string sourceName, Dictionary<string, object> options) | |
{ | |
AudioSource soundPlayer; | |
object option; | |
SoundsManager.playlist.TryGetValue (sourceName,out soundPlayer); | |
if (soundPlayer!=null) | |
{ | |
if (soundPlayer.volume == 0) | |
soundPlayer.volume = 0.1F; | |
audioEvent.Remove (sourceName); | |
audioEvent.Add (sourceName,AUDIO_OPT_FADEIN); | |
//Options: Fade In Volume | |
float fadeInVolume; | |
if(options.ContainsKey(AUDIO_OPT_VOLUME)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_VOLUME,out option); | |
fadeInVolume = float.Parse (option.ToString()); | |
}else | |
fadeInVolume = 1; | |
//Options: Fade In Time | |
float fadeInTime; | |
if(options.ContainsKey(AUDIO_OPT_VOLUME)) | |
{ | |
option = null; | |
options.TryGetValue(AUDIO_OPT_VOLUME,out option); | |
float.TryParse (option.ToString(),out fadeInTime); | |
}else | |
fadeInTime = 0.1F; | |
//Start | |
iTween.AudioTo(gameObject,iTween.Hash ( | |
"audiosource",soundPlayer, | |
"volume",fadeInVolume, | |
"time",fadeInTime)); | |
}else | |
{ | |
Debug.Log ("FadeInSound ... "+sourceName+" not found"); | |
} | |
SoundsManager.printAudioEvents (); | |
} | |
public void StopSound(string sourceName) | |
{ | |
AudioSource player; | |
SoundsManager.playlist.TryGetValue (sourceName,out player); | |
if(player!=null) | |
{ | |
player.volume = 0; | |
Debug.Log ("Audio "+player.clip.name+" has stopped"); | |
}else | |
{ | |
Debug.Log ("StopSound ... "+sourceName+" not found"); | |
} | |
} | |
public bool isAudioEventRunning(string sourceName, string eventNameCompare) | |
{ | |
string sourceEventName = ""; | |
audioEvent.TryGetValue (sourceName, out sourceEventName); | |
return sourceEventName == eventNameCompare; | |
} | |
IEnumerator PitchTimeout(AudioSource soundPlayer,int time) | |
{ | |
yield return new WaitForSeconds(time); | |
soundPlayer.pitch = 1; | |
} | |
} | |
/* | |
CREDIT | |
ihsanberahim.com | |
fiction-labs.com | |
pkmedia.com.my | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need someone helps me find why "FadeOutSound" somehow not work.