2using System.Collections.Generic;
3using UnityEngine.SceneManagement;
78 instance = (
new GameObject(
"EazySoundManager")).AddComponent<EazySoundManager>();
101 audioPool =
new Dictionary<int, Audio>();
113 DontDestroyOnLoad(
this);
154 Dictionary<int, Audio> audioDict =
new Dictionary<int, Audio>();
198 List<int> keys =
new List<int>(audioDict.Keys);
200 foreach (
int key
in keys)
202 Audio audio = audioDict[key];
215 audioDict.Remove(key);
227 List<int> keys =
new List<int>(audioDict.Keys);
228 foreach (
int key
in keys)
230 Audio audio = audioDict[key];
234 audioDict.Remove(key);
240 foreach (
int key
in keys)
261 audioDict.Add(audioID,
audioPool[audioID]);
270 #region GetAudio Functions
434 if (audioDict.ContainsKey(audioID))
436 return audioDict[audioID];
439 if (usePool &&
audioPool.ContainsKey(audioID) &&
audioPool[audioID].Type == audioType)
451 var audio = audioDict.SingleOrDefault(x => x.Value.Name == name);
460 List<int> audioTypeKeys =
new List<int>(audioDict.Keys);
461 List<int> poolKeys =
new List<int>(
audioPool.Keys);
462 List<int> keys = usePool ? audioTypeKeys.Concat(poolKeys).ToList() : audioTypeKeys;
463 foreach (
int key
in keys)
466 if (audioDict.ContainsKey(key))
468 audio = audioDict[key];
480 if (audio.
Clip == audioClip && audio.
Type == audioType)
491 #region Prepare Function
522 public static int PrepareMusic(AudioClip clip,
float volume,
bool loop,
bool persist)
537 public static int PrepareMusic(AudioClip clip,
float volume,
bool loop,
bool persist,
float fadeInSeconds,
538 float fadeOutSeconds)
556 public static int PrepareMusic(AudioClip clip,
float volume,
bool loop,
bool persist,
float fadeInSeconds,
557 float fadeOutSeconds,
float currentMusicfadeOutSeconds, Transform sourceTransform)
560 currentMusicfadeOutSeconds, sourceTransform);
579 public static int PrepareSound(AudioClip clip,
string name,
float volume)
603 public static int PrepareSound(AudioClip clip,
string name,
float volume,
bool loop, Transform sourceTransform)
630 bool persist,
float fadeInSeconds,
float fadeOutSeconds,
float currentMusicfadeOutSeconds,
631 Transform sourceTransform)
635 Debug.LogError(
"[Eazy Sound Manager] Audio clip is null", clip);
641 if (ignoreDuplicateAudio)
644 if (duplicateAudio !=
null)
651 Audio audio =
new Audio(name, audioType, clip, loop, persist, volume, fadeInSeconds, fadeOutSeconds,
655 audioDict.Add(audio.
AudioID, audio);
661 bool persist,
float fadeInSeconds,
float fadeOutSeconds,
float currentMusicfadeOutSeconds,
662 Transform sourceTransform)
666 Debug.LogError(
"[Eazy Sound Manager] Audio clip is null", clip);
672 if (ignoreDuplicateAudio)
675 if (duplicateAudio !=
null)
682 Audio audio =
new Audio(
string.Empty, audioType, clip, loop, persist, volume, fadeInSeconds, fadeOutSeconds,
686 audioDict.Add(audio.
AudioID, audio);
693 #region Play Functions
711 public static int PlayMusic(AudioClip clip,
float volume)
724 public static int PlayMusic(AudioClip clip,
float volume,
bool loop,
bool persist)
739 public static int PlayMusic(AudioClip clip,
float volume,
bool loop,
bool persist,
float fadeInSeconds,
740 float fadeOutSeconds)
758 public static int PlayMusic(AudioClip clip,
float volume,
bool loop,
bool persist,
float fadeInSeconds,
759 float fadeOutSeconds,
float currentMusicfadeOutSeconds, Transform sourceTransform)
762 currentMusicfadeOutSeconds, sourceTransform);
770 public static int PlaySound(AudioClip clip,
string name,
float volume)
781 public static int PlaySound(AudioClip clip,
string name,
bool loop)
786 public static int PlaySound(AudioClip clip,
string name,
float volume,
bool loop)
799 public static int PlaySound(AudioClip clip,
string name,
float volume,
bool loop, Transform sourceTransform)
826 float fadeInSeconds,
float fadeOutSeconds,
float currentMusicfadeOutSeconds, Transform sourceTransform)
828 int audioID =
PrepareAudio(audioType, clip, volume, loop, persist, fadeInSeconds, fadeOutSeconds,
829 currentMusicfadeOutSeconds, sourceTransform);
843 float fadeInSeconds,
float fadeOutSeconds,
float currentMusicfadeOutSeconds, Transform sourceTransform)
845 int audioID =
PrepareAudio(name,audioType, clip, volume, loop, persist, fadeInSeconds, fadeOutSeconds,
846 currentMusicfadeOutSeconds, sourceTransform);
861 #region Stop Functions
875 public static void StopAll(
float musicFadeOutSeconds)
919 List<int> keys =
new List<int>(audioDict.Keys);
920 foreach (
int key
in keys)
922 Audio audio = audioDict[key];
923 if (fadeOutSeconds > 0)
925 audio.FadeOutSeconds = fadeOutSeconds;
934 #region Pause Functions
974 List<int> keys =
new List<int>(audioDict.Keys);
975 foreach (
int key
in keys)
977 Audio audio = audioDict[key];
984 #region Resume Functions
1024 List<int> keys =
new List<int>(audioDict.Keys);
1025 foreach (
int key
in keys)
1027 Audio audio = audioDict[key];
void Play()
Start playing audio clip from the beginning.
AudioSource AudioSource
The audio source that is responsible for this audio. Do not modify the audiosource directly as it cou...
bool IsPlaying
Whether the audio is currently playing.
void Update()
Update loop of the Audio. This is automatically called from the sound manager itself....
bool Paused
Whether the audio is paused.
void Stop()
Stop playing audio clip.
AudioClip Clip
Audio clip to play/is playing.
void Pause()
Pause playing audio clip.
AudioType Type
The type of the Audio.
AudioType
Enum representing the type of audio.
void Resume()
Resume playing audio clip.
bool Activated
Whether the audio is created and updated at least once.
int AudioID
The ID of the Audio.
bool Persist
Whether the audio persists in between scene changes.
Static class responsible for playing and managing audio and sounds.
static int PrepareAudio(string name, Audio.AudioType audioType, AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
static void PauseAllMusic()
Pause all music playing.
static void RemoveNonPersistAudio(Dictionary< int, Audio > audioDict)
Remove all non-persistant audios from an audio dictionary.
static void ResumeAll()
Resume all audio playing.
static int PrepareSound(AudioClip clip, string name, float volume, bool loop, Transform sourceTransform)
Prepares and initializes a sound fx.
static Audio GetSoundAudio(AudioClip audioClip)
Returns the first occurrence of sound Audio that plays the given audioClip. Returns null if no such A...
static int PrepareUISound(AudioClip clip, string name)
Prepares and initializes a UI sound fx.
static void ResumeAllUISounds()
Resume all UI sound fx playing.
static void StopAllUISounds()
Stop all UI sound fx playing.
static Audio GetAudio(AudioClip audioClip)
Returns the first occurrence of Audio that plays the given audioClip. Returns null if no such Audio i...
static void StopAllMusic(float fadeOutSeconds)
Stop all music playing.
static float GlobalSoundsVolume
Global sounds volume.
static Dictionary< int, Audio > audioPool
static int PrepareSound(AudioClip clip, string name, bool loop)
Prepares and initializes a sound fx.
static Dictionary< int, Audio > soundsAudio
static Audio GetUISoundAudio(AudioClip audioClip)
Returns the first occurrence of UI sound Audio that plays the given audioClip. Returns null if no suc...
static int PlayMusic(AudioClip clip, float volume)
Play background music.
static Audio GetAudio(Audio.AudioType audioType, bool usePool, AudioClip audioClip)
static int PlayMusic(AudioClip clip)
Play background music.
static bool RestoreAudioFromPool(Audio.AudioType audioType, int audioID)
Restores and re-adds a pooled audio to its corresponding audio dictionary.
static bool IgnoreDuplicateMusic
When set to true, new music audios that have the same audio clip as any other music audios,...
static int PrepareMusic(AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
Prepares and initializes background music.
static int PrepareSound(AudioClip clip, string name, float volume)
Prepares and initializes a sound fx.
static int PlaySound(AudioClip clip, string name, float volume, bool loop)
static bool IgnoreDuplicateUISounds
When set to true, new UI sound audios that have the same audio clip as any other UI sound audios,...
static EazySoundManager instance
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
Event triggered when a new scene is loaded.
static Audio GetUISoundAudio(string name)
static void StopAllMusic()
Stop all music playing.
static int PlaySound(AudioClip clip, string name, float volume)
static int PlaySound(AudioClip clip, string name, float volume, bool loop, Transform sourceTransform)
Play a sound fx.
static bool GetAudioTypeIgnoreDuplicateSetting(Audio.AudioType audioType)
Retrieves the IgnoreDuplicates setting of audios of a specified audio type.
static float GlobalVolume
Global volume.
static float GlobalUISoundsVolume
Global UI sounds volume.
static int PrepareUISound(AudioClip clip, string name, float volume)
Prepares and initializes a UI sound fx.
static int PlaySound(AudioClip clip, string name)
static GameObject Gameobject
The gameobject that the sound manager is attached to.
static Audio GetSoundAudio(string name)
static int PrepareSound(AudioClip clip, string name)
Prepares and initializes a sound fx.
static int PrepareMusic(AudioClip clip, float volume, bool loop, bool persist)
Prepares and initializes background music.
static void StopAll(float musicFadeOutSeconds)
Stop all audio playing.
static void StopAllAudio(Audio.AudioType audioType, float fadeOutSeconds)
static void StopAll()
Stop all audio playing.
static int PrepareMusic(AudioClip clip, float volume)
Prepares and initializes background music.
static int PrepareMusic(AudioClip clip)
Prepares and initializes background music.
static int PlayUISound(AudioClip clip)
Play a UI sound fx.
static int PlayMusic(AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds)
Play background music.
static bool IgnoreDuplicateSounds
When set to true, new sound audios that have the same audio clip as any other sound audios,...
static void PauseAllUISounds()
Pause all UI sound fx playing.
static void UpdateAllAudio(Dictionary< int, Audio > audioDict)
Updates the state of all audios of an audio dictionary.
static Action< Audio > AudioStopped
static void ResumeAllAudio(Audio.AudioType audioType)
static Audio GetMusicAudio(AudioClip audioClip)
Returns the first occurrence of music Audio that plays the given audioClip. Returns null if no such A...
static Audio GetSoundAudio(int audioID)
Returns the sound fx Audio that has as its id the audioID if one is found, returns null if no such Au...
static Audio GetMusicAudio(int audioID)
Returns the music Audio that has as its id the audioID if one is found, returns null if no such Audio...
static Dictionary< int, Audio > UISoundsAudio
static int PlaySound(AudioClip clip, string name, bool loop)
Play a sound fx.
static void ResumeAllMusic()
Resume all music playing.
static int PlayUISound(AudioClip clip, float volume)
Play a UI sound fx.
static Audio GetMusicAudio(string name)
static void PauseAllSounds()
Pause all sound fx playing.
static Audio GetAudio(int audioID)
Returns the Audio that has as its id the audioID if one is found, returns null if no such Audio is fo...
static int PrepareAudio(Audio.AudioType audioType, AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
static void StopAllSounds()
Stop all sound fx playing.
static void PauseAllAudio(Audio.AudioType audioType)
static int PlayMusic(AudioClip clip, float volume, bool loop, bool persist)
Play background music.
static int PlayAudio(Audio.AudioType audioType, AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
static Audio GetUISoundAudio(int audioID)
Returns the UI sound fx Audio that has as its id the audioID if one is found, returns null if no such...
static void ResumeAllSounds()
Resume all sound fx playing.
static Dictionary< int, Audio > musicAudio
static int PlayMusic(AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
Play background music.
static int PrepareMusic(AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds)
Prerpares and initializes background music.
static Dictionary< int, Audio > GetAudioTypeDictionary(Audio.AudioType audioType)
Retrieves the audio dictionary based on the audioType.
static float GlobalMusicVolume
Global music volume.
static EazySoundManager()
static Audio GetAudio(string name)
void Init()
Initialized the sound manager.
static Audio GetAudio(Audio.AudioType audioType, bool usePool, int audioID)
static void PauseAll()
Pause all audio playing.
static Audio GetAudio(Audio.AudioType audioType, bool usePool, string name)
static int PlayAudio(string name, Audio.AudioType audioType, AudioClip clip, float volume, bool loop, bool persist, float fadeInSeconds, float fadeOutSeconds, float currentMusicfadeOutSeconds, Transform sourceTransform)
static EazySoundManager Instance