public AudioOneShotPlayer PlayOneShotAtPosition(AudioClip clip, Vector3 position, float volumeMultiplier = 1) { Vector2 delta = position - Listener.transform.position; if (delta.sqrMagnitude > soundInRange * soundInRange) { return(null); } AudioOneShotPlayer player = oneShotPlayerPool.Get(); player.transform.position = position; player.Play(clip, (_player) => oneShotPlayerPool.Put(_player), volumeMultiplier); return(player); }
protected virtual void Awake() { clipDicts = new Dictionary <AudioIDEnum, AudioPreset.EnumToAudio>(); if (oneShotAudioSourcePrefab == null) { oneShotPlayerPool = new PrefabPool <AudioOneShotPlayer>(delegate { GameObject obj = new GameObject("AudioOneShotPlayer"); AudioOneShotPlayer player = obj.AddComponent <AudioOneShotPlayer>(); player.Volume = oneShotAudioSrc.volume; return(player); }, true, "AudioCollects"); } else { oneShotPlayerPool = new PrefabPool <AudioOneShotPlayer>(oneShotAudioSourcePrefab, true, "AudioCollects"); } if (defaultPreset != null) { LoadAudioPreset(defaultPreset); } if (oneShotAudioSrc == null) { oneShotAudioSrc = gameObject.AddComponent <AudioSource>(); } if (bgmAudioSrc == null) { bgmAudioSrc = gameObject.AddComponent <AudioSource>(); bgmAudioSrc.loop = true; } if (secondaryBgmAudioSrc == null) { secondaryBgmAudioSrc = gameObject.AddComponent <AudioSource>(); secondaryBgmAudioSrc.loop = true; } loopPlayers = new List <OneShotLoopPlayer>(); }
public AudioOneShotPlayer PlayOneShotAtPosition(AudioIDEnum ID, Vector3 position, float volumeMultiplier = 1) { if (!clipDicts.ContainsKey(ID)) { #if UNITY_EDITOR Debug.LogWarningFormat("Audio '{0}' doesn't exist", ID); #endif return(null); } Vector2 delta = position - Listener.transform.position; if (delta.sqrMagnitude > soundInRange * soundInRange) { return(null); } AudioOneShotPlayer player = oneShotPlayerPool.Get(); player.transform.position = position; player.Play(clipDicts[ID].Clip, (_player) => oneShotPlayerPool.Put(_player), clipDicts[ID].Volume * volumeMultiplier); return(player); }