// Use this for initialization void Start() { if (_source == null) { _source = AudioManager.Instance.Play(Song, AudioPoolType.Music, Constants.Mixer.MixerGroups.Master.BackgroundMusic.Name, true, () => { _source = null; }); } }
private void _populatePool(AudioPoolType g) { while (_inactiveSources[g].Count < _poolSizes[g]) { _inactiveSources[g].Push(PooledAudioSource.CreateInstance(this.transform, g)); } }
public override PlayingEvent Play() { // Setting weights if needed float[] weights = _givenWeights; if (!_specifyWeights || _clips.Length != _givenWeights.Length) { weights = new float[_clips.Length]; for (int i = 0; i < _clips.Length; ++i) { weights[i] = 1.0f / (float)_clips.Length; } } Debug.Log(string.Format("Playing the audio event -> {0}", _eventName)); PlayingEvent e = new PlayingEvent(this, Time.time); PooledAudioSource pooled = AudioManager.Instance.PoolSource(_channel); pooled.Source.clip = SelectRandomClip(weights); e.Sources.Add(pooled); _playing.Add(e); _playingRoutine = AudioManager.Instance.StartCoroutine(WaitForEndOfEvent(e)); return(e); }
protected void InitAudioSettings() { var audioSettings = LoadAudioSettings(); //MusicManager.instance.volume = audioSettings.musicVolume; PooledAudioSource.SetGlobalVolume(audioSettings.soundFxVolume); }
protected void InitUIFromAudioManagers() { //sliderMusicVolume.value = MusicManager.instance.volume; muteSoundFxValueChangedSoundFeedback = true; sliderSoundFxVolume.value = PooledAudioSource.GetGlobalVolume(); muteSoundFxValueChangedSoundFeedback = false; }
public void ReturnSource(PooledAudioSource pooled) { pooled.TimeStamp = float.MinValue; pooled.InUse = false; pooled.Source.transform.SetParent(_poolGOs[(int)pooled.Channel].transform, false); pooled.Source.transform.SetAsLastSibling(); pooled.Source.name = string.Format(SOURCE_NAME_FORMAT, pooled.Channel.ToString()); }
public static PooledAudioSource CreateInstance(Transform parent, AudioPoolType g) { GameObject obj = new GameObject("[" + Enum.GetName(typeof(AudioPoolType), g) + "] PooledAudioSource"); PooledAudioSource inst = obj.AddComponent <PooledAudioSource>(); inst._group = g; inst.Source = inst.gameObject.AddComponent <AudioSource>(); inst.transform.SetParent(parent); inst.ResetValues(); // init values return(inst); }
/// <summary> /// Plays a clip and returns the active audio source (for later reference). Returns null if no sources available in the pool. /// </summary> public PooledAudioSource Play(AudioClip clip, AudioPoolType group, string mixerGroup, float volume, float pitch, bool loop, float delay, Action onReturnedToPool) { PooledAudioSource source = _getFromPool(group); if (source == null) { return(null); } source.Init(clip, mixerGroup, volume, pitch, loop, onReturnedToPool); source.Play(delay); return(source); }
private PooledAudioSource _getFromPool(AudioPoolType g) { if (_inactiveSources[g].Count > 0) { PooledAudioSource source = _inactiveSources[g].Pop(); _activeSources[g].Add(source); return(source); } else { Debug.LogWarning("[AudioManager] Audio source pool is empty! Consider increasing the size of the pool to play all these dang sounds."); return(null); } }
public override PlayingEvent Play() { Debug.Log(string.Format("Playing the audio event -> {0}", _eventName)); PlayingEvent e = new PlayingEvent(this, Time.time); PooledAudioSource pooled = AudioManager.Instance.PoolSource(_channel); pooled.Source.clip = _clip; e.Sources.Add(pooled); _playing.Add(e); _playingRoutine = AudioManager.Instance.StartCoroutine(WaitForEndOfEvent(e)); return(e); }
/// <summary> /// Setup the audio loops on the client and the server /// </summary> private void _initAudio() { if (PhotonNetwork.player.IsMasterClient) { // TODO: Play main game loop that will play on the server if (_musicSource == null) { _musicSource = AudioManager.Instance.Play(ServerMusic, AudioPoolType.Music, Constants.Mixer.Mixers.Master.Music.Name, true, () => { _musicSource = null; }); } } else { // TODO: This audio will play on the client } }
private void _returnToPool(PooledAudioSource source) { source.OnReturned(); _activeSources[source.Group].Remove(source); float currentPoolSize = _inactiveSources[source.Group].Count + _activeSources[source.Group].Count; if (currentPoolSize >= _poolSizes[source.Group]) // destroy the source if there's too many in pool { Destroy(source.gameObject); //TODO: move to "none" pool instead of destroying? } else { _inactiveSources[source.Group].Push(source); } }
public PooledAudioSource PoolSource(AudioChannel channel) { IOrderedEnumerable <PooledAudioSource> usable = _poolSources[channel].Where(p => !p.InUse).OrderBy(p => p.TimeStamp); if (usable.Count() <= 0) { usable = _poolSources[channel].OrderBy(p => p.TimeStamp); } PooledAudioSource next = usable.First(); next.Source.name = string.Format(SOURCE_NAME_FORMAT_INUSE, next.Channel.ToString()); next.InUse = true; next.TimeStamp = Time.time; return(next); }
protected void Awake() { DontDestroyOnLoad(this.gameObject); // Creating base buckets for audio sources pooling _poolGOs = new GameObject[Enum.GetValues(typeof(AudioChannel)).Length]; _poolSources = new Dictionary <AudioChannel, List <PooledAudioSource> >(); foreach (AudioChannel channel in Enum.GetValues(typeof(AudioChannel))) { _poolSources.Add(channel, new List <PooledAudioSource>()); GameObject go = new GameObject(channel.ToString()); go.transform.SetParent(this.transform, false); for (int i = 0; i < _database.GetMaxCountForChannel(channel); ++i) { AudioSource source; if (_database.BaseSource != null) { source = Instantiate(_database.BaseSource); source.name = string.Format(SOURCE_NAME_FORMAT, channel.ToString()); } else { source = new GameObject(string.Format(SOURCE_NAME_FORMAT, channel.ToString()), typeof(AudioSource)).GetComponent <AudioSource>(); } source.transform.SetParent(go.transform, false); PooledAudioSource pooled = new PooledAudioSource { Channel = channel, Source = source, TimeStamp = float.MinValue, InUse = false }; _poolSources[channel].Add(pooled); } _poolGOs[(int)channel] = go; } }
void OnEnable() { if (clips != null) { if (is2D) { audioSource = SoundManager.PlayAudio(clips.audioClips, group, volume, looped, Settings); } else { audioSource = SoundManager.PlayAudioAtPoint(clips.audioClips, transform.position, group, volume, looped, Settings); } if (looped && randomStart && audioSource != null) { audioSource.RandomTime(); } } if (asset != null) { if (is2D) { instance = asset.Play(Settings, volume, group, looped); } else { instance = asset.PlayAtPoint(Settings, transform.position, volume, group, looped); } if (looped && randomStart && instance != null) { instance.RandomTime(); } } }
public void ReturnSource(PooledAudioSource pooled) { _component.ReturnSource(pooled); }