Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class AudioManager
- {
- //Music
- private static AudioClip _currentSong;
- private static AudioSource _musicSource;
- private static float _musicVolume = 1.0f;
- private static bool _muteMusic;
- public static float MusicVolume
- {
- get
- {
- if (_muteMusic) return 0;
- return _musicVolume;
- }
- set
- {
- _musicVolume = Mathf.Clamp(value, 0, 1);
- UpdateMusicVolume();
- }
- }
- public static float UnmutedMusicVolume
- {
- get { return _musicVolume; }
- }
- public static bool MuteMusic
- {
- get { return _muteMusic; }
- set
- {
- _muteMusic = value;
- UpdateMusicVolume();
- }
- }
- //Sounds
- private static List<GameAudio> _gameAudios = new List<GameAudio>();
- private static Queue<GameObject> _oneShotAudioPool = new Queue<GameObject>();
- private static GameObject _oneShotAudioObject;
- private static float _soundVolume = 1.0f;
- private static bool _muteSound;
- public static float SoundVolume
- {
- get
- {
- if (_muteSound) return 0;
- return _soundVolume;
- }
- set
- {
- _soundVolume = Mathf.Clamp(value, 0, 1);
- UpdateSoundVolume();
- }
- }
- public static float UnmutedSoundVolume
- {
- get { return _soundVolume; }
- }
- public static bool MuteSound
- {
- get { return _muteSound; }
- set
- {
- _muteSound = value;
- UpdateSoundVolume();
- }
- }
- //Music
- /// <summary>
- /// Will play last song if song is null
- /// </summary>
- /// <param name="song">Can be null</param>
- public static void PlayMusic(AudioClip song)
- {
- var differentSong = false;
- if (song != _currentSong)
- {
- _currentSong = song;
- differentSong = true;
- }
- if (_musicSource)
- {
- if (_currentSong)
- {
- _musicSource.volume = MusicVolume;
- if (differentSong)
- {
- _musicSource.clip = _currentSong;
- _musicSource.Play();
- }
- else if (!_musicSource.isPlaying) _musicSource.Play();
- }
- else
- {
- _musicSource.Stop();
- _musicSource.clip = null;
- }
- }
- }
- public static void SetMusicSource(GameObject musicSource)
- {
- if (_musicSource) return;
- var newMusicSourceObject = Object.Instantiate(musicSource);
- Object.DontDestroyOnLoad(newMusicSourceObject);
- _musicSource = newMusicSourceObject.GetComponent<AudioSource>();
- if (_musicSource.clip) _musicSource.Play();
- }
- public static void PauseMusic(bool pause)
- {
- if (!_musicSource) return;
- if (pause)
- {
- _musicSource.Pause();
- }
- else
- {
- _musicSource.UnPause();
- }
- }
- public static void UpdateMusicVolume()
- {
- if (_musicSource != null)
- {
- _musicSource.volume = MusicVolume;
- }
- }
- //Sounds
- //Get random pitch
- public static float GetPitchByRangeShift(float min, float max)
- {
- if (max < min)
- {
- var temp = min;
- min = max;
- max = temp;
- }
- float pitch = Random.Range(min, max);
- if (pitch < 0)
- {
- pitch = 1/(1 - pitch);
- }
- else
- {
- pitch += 1;
- }
- return pitch;
- }
- /// <summary>
- /// Is for audio that can also be played in reverse
- /// </summary>
- public static void PlayNormal(AudioSource audio, float pitch = 1.0f)
- {
- audio.pitch = pitch;
- if (audio.isPlaying) return;
- audio.timeSamples = 0;
- audio.Play();
- }
- public static void PlayInReverse(AudioSource audio, float pitch = 1.0f)
- {
- audio.pitch = -pitch;
- if (audio.isPlaying) return;
- audio.timeSamples = audio.clip.samples - 1;
- audio.Play();
- }
- public static void PrepareOneShotAudioPool(int amountPrepooled, GameObject oneShotAudioObject)
- {
- if (_oneShotAudioPool == null) _oneShotAudioPool = new Queue<GameObject>();
- else _oneShotAudioPool.Clear();
- if (oneShotAudioObject)
- {
- if (oneShotAudioObject.GetComponent<OneShotAudio>()) _oneShotAudioObject = oneShotAudioObject;
- }
- for (int i = 0; i < amountPrepooled; i++)
- {
- var spawnedObject = Object.Instantiate(_oneShotAudioObject);
- if (spawnedObject == null) break;
- PutBackOnOneShotAudioPool(spawnedObject.GetComponent<OneShotAudio>());
- }
- }
- public static void PutBackOnOneShotAudioPool(OneShotAudio oneShotAudio)
- {
- if (oneShotAudio)
- {
- _oneShotAudioPool.Enqueue(oneShotAudio.gameObject);
- oneShotAudio.gameObject.SetActive(false);
- }
- }
- public static OneShotAudio GetOneShotAudio(Vector3 position)
- {
- if (_oneShotAudioObject == null)
- {
- Debug.LogError("No OneShotAudioObject source founded");
- return null;
- }
- GameObject oneShotAudio = null;
- bool succes = false;
- while (_oneShotAudioPool.Count > 0)
- {
- oneShotAudio = _oneShotAudioPool.Dequeue();
- if (oneShotAudio == null) continue;
- var script = oneShotAudio.GetComponent<OneShotAudio>();
- if (script == null)
- {
- Object.Destroy(oneShotAudio);
- continue;
- }
- oneShotAudio.SetActive(true);
- script.Reset();
- succes = true;
- break;
- }
- if (!succes) oneShotAudio = Object.Instantiate(_oneShotAudioObject);
- oneShotAudio.transform.position = position;
- return oneShotAudio.GetComponent<OneShotAudio>();
- }
- public static void PauseSounds(bool pause)
- {
- if (pause)
- {
- CleanAudioList();
- for (int i = 0; i < _gameAudios.Count; i++)
- {
- var gameAudio = _gameAudios[i];
- if (!gameAudio.AudioSource) continue;
- if (gameAudio.AudioSource.isPlaying) gameAudio.AudioSource.Pause();
- }
- }
- else
- {
- CleanAudioList();
- for (int i = 0; i < _gameAudios.Count; i++)
- {
- var gameAudio = _gameAudios[i];
- if (gameAudio.AudioSource == null) continue;
- gameAudio.AudioSource.UnPause();
- }
- }
- }
- public static void AddGameAudio(GameAudio gameAudio)
- {
- gameAudio.UpdateVolume();
- _gameAudios.Add(gameAudio);
- }
- public static void RemoveGameAudio(GameAudio gameAudio)
- {
- _gameAudios.Remove(gameAudio);
- }
- public static void InitializeAudioList()
- {
- if (_gameAudios != null) _gameAudios.Clear();
- else _gameAudios = new List<GameAudio>();
- }
- public static void CleanAudioList()
- {
- if (_gameAudios == null) _gameAudios = new List<GameAudio>();
- _gameAudios.RemoveAll(item => item == null);
- for (int i = 0; i < _gameAudios.Count - 1; i++)
- {
- for (int j = i + 1; j < _gameAudios.Count;)
- {
- if (_gameAudios[i] == _gameAudios[j])
- {
- _gameAudios.RemoveAt(j);
- }
- else j++;
- }
- }
- }
- public static void UpdateSoundVolume()
- {
- bool requestClean = false;
- for (int i = 0; i < _gameAudios.Count; i++)
- {
- var gameAudio = _gameAudios[i];
- if (gameAudio == null)
- {
- requestClean = true;
- continue;
- }
- if (gameAudio.AudioSource != null)
- {
- gameAudio.UpdateVolume();
- }
- }
- if (requestClean) CleanAudioList();
- }
- }
- [Serializable]
- public class GameAudio
- {
- [SerializeField]
- private AudioSource _audioSource;
- [SerializeField]
- private float _unscaledVolume=1.0f;
- public AudioSource AudioSource
- {
- get { return _audioSource; }
- set
- {
- _audioSource = value;
- UpdateVolume();
- }
- }
- public AudioClip AudioClip
- {
- get
- {
- if (_audioSource == null) return null;
- return _audioSource.clip;
- }
- set
- {
- if (_audioSource == null)
- {
- Debug.LogError("There is no audio source, so clip can't be set!");
- return;
- }
- _audioSource.clip = value;
- }
- }
- public float UnscaledVolume
- {
- get { return _unscaledVolume; }
- set
- {
- _unscaledVolume = value;
- UpdateVolume();
- }
- }
- public GameAudio(AudioSource audioSource, float unscaledVolume=1.0f)
- {
- _audioSource = audioSource;
- _unscaledVolume = unscaledVolume;
- GenericUpdateVolume();
- }
- private float GenericUpdateVolume()
- {
- if (AudioSource == null) return -1;
- AudioSource.volume = UnscaledVolume * AudioManager.SoundVolume;
- return AudioSource.volume;
- }
- public virtual float UpdateVolume()
- {
- return GenericUpdateVolume();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement