protected ulong ConfigurePoolObject(int poolIndex, string track, AudioClip clip, Vector3 position, float volume, float spatialBlend, float unimportance, float startTime, bool ignoreAudioListenerPause = false) { if (poolIndex < 0 || poolIndex >= _pool.Count) { return(0); } AudioPoolItem poolItem = _pool[poolIndex]; _idGiver++; AudioSource source = poolItem.AudioSource; source.clip = clip; source.volume = volume; source.spatialBlend = spatialBlend; source.ignoreListenerPause = ignoreAudioListenerPause; source.outputAudioMixerGroup = _tracks[track].Group; source.transform.position = position; poolItem.Playing = true; poolItem.Unimportance = unimportance; poolItem.ID = _idGiver; source.time = Mathf.Min(startTime, source.clip.length); poolItem.GameObject.SetActive(true); source.Play(); poolItem.Coroutine = StopSoundDelayed(_idGiver, source.clip.length); StartCoroutine(poolItem.Coroutine); _activePool.Add(_idGiver, poolItem); return(_idGiver); }
void Awake() { DontDestroyOnLoad(gameObject); if (_mixer == null) { return; } // By passing in string.Empty, we are getting all the mixer groups AudioMixerGroup[] groups = _mixer.FindMatchingGroups(string.Empty); foreach (AudioMixerGroup group in groups) { TrackInfo track = new TrackInfo(); track.Name = group.name; track.Group = group; track.TrackFader = null; _tracks.Add(group.name, track); } for (int i = 0; i < _maxSounds; i++) { GameObject o = new GameObject("Pool Item"); AudioSource audioSource = o.AddComponent <AudioSource>(); o.transform.parent = transform; AudioPoolItem item = new AudioPoolItem(); item.GameObject = o; item.AudioSource = audioSource; item.Transform = o.transform; item.Playing = false; o.SetActive(false); _pool.Add(item); } }
public ulong PlayOneShotSound(string track, AudioClip clip, Vector3 position, float volume, float spatialBlend, int priority = 128) { if (!tracks.ContainsKey(track) || clip == null || volume.Equals(0.0f)) { return(0); } float unimportance = (listenerPosition.position - position).sqrMagnitude / Mathf.Max(1, priority); int leastImportantIndex = -1; float leastImportanceValue = float.MaxValue; // Find an available audio source for (int i = 0; i < pool.Count; i++) { AudioPoolItem poolItem = pool[i]; if (!poolItem.playing) { return(ConfigurePoolObject(i, track, clip, position, volume, spatialBlend, unimportance)); } else if (poolItem.unimportance > leastImportanceValue) { leastImportanceValue = poolItem.unimportance; leastImportantIndex = i; } } if (leastImportanceValue > unimportance) { return(ConfigurePoolObject(leastImportantIndex, track, clip, position, volume, spatialBlend, unimportance)); } return(0); }
// ------------------------------------------------------------------------------- // Name : ConfigurePoolObject // Desc : Used internally to configure a pool object // ------------------------------------------------------------------------------- protected ulong ConfigurePoolObject(int poolIndex, string track, AudioClip clip, Vector3 position, float volume, float spatialBlend, float unimportance, float startTime, bool ignoreListenerPause) { // If poolIndex is out of range abort request if (poolIndex < 0 || poolIndex >= _pool.Count) { return(0); } // Get the pool item AudioPoolItem poolItem = _pool[poolIndex]; // Stop any previously playing coroutine if (poolItem.Coroutine != null) { StopCoroutine(poolItem.Coroutine); } // Generate new ID so we can stop it later if we want to _idGiver++; // Configure the audio source's position and colume AudioSource source = poolItem.AudioSource; source.clip = clip; source.volume = volume; source.spatialBlend = spatialBlend; source.ignoreListenerPause = ignoreListenerPause; // Assign to requested audio group/track source.outputAudioMixerGroup = _tracks[track].Group; // Position source at requested position source.transform.position = position; // Enable GameObject and record that it is now playing poolItem.Playing = true; poolItem.Unimportance = unimportance; poolItem.ID = _idGiver; source.time = Mathf.Min(startTime, source.clip.length); poolItem.GameObject.SetActive(true); source.Play(); poolItem.Coroutine = StopSoundDelayed(_idGiver, source.clip.length); StartCoroutine(poolItem.Coroutine); // Add this sound to our active pool with its unique id _activePool[_idGiver] = poolItem; // Return the id to the caller return(_idGiver); }
// ------------------------------------------------------------------------------- // Name : PlayOneShotSound // Desc : Scores the priority of the sound and search for an unused pool item // to use as the audio source. If one is not available an audio source // with a lower priority will be killed and reused // ------------------------------------------------------------------------------- public ulong PlayOneShotSound(string track, AudioClip clip, Vector3 position, float volume, float spatialBlend, int priority = 128, float startTime = 0.0f, bool ignoreListenerPause = false) { // Do nothing if track does not exist, clip is null or volume is zero if (!_tracks.ContainsKey(track) || clip == null || volume.Equals(0.0f)) { return(0); } float unimportance = (_listenerPos.position - position).sqrMagnitude / Mathf.Max(1, priority); int leastImportantIndex = -1; float leastImportanceValue = float.MinValue; // Find an available audio source to use for (int i = 0; i < _pool.Count; i++) { AudioPoolItem poolItem = _pool[i]; // Is this source available if (!poolItem.Playing) { return(ConfigurePoolObject(i, track, clip, position, volume, spatialBlend, unimportance, startTime, ignoreListenerPause)); } else // We have a pool item that is less important than the one we are going to play if (poolItem.Unimportance > leastImportanceValue) { // Record the least important sound we have found so far // as a candidate to relace with our new sound request leastImportanceValue = poolItem.Unimportance; leastImportantIndex = i; } } // If we get here all sounds are being used but we know the least important sound currently being // played so if it is less important than our sound request then use replace it if (leastImportanceValue > unimportance) { return(ConfigurePoolObject(leastImportantIndex, track, clip, position, volume, spatialBlend, unimportance, startTime, ignoreListenerPause)); } // Could not be played (no sound in the pool available) return(0); }
void Awake() { // This object must live for the entire application DontDestroyOnLoad(gameObject); // Return if we have no valid mixer reference if (!_mixer) { return; } // Fetch all the groups in the mixer - These will be our mixers tracks AudioMixerGroup[] groups = _mixer.FindMatchingGroups(string.Empty); // Create our mixer tracks based on group name (Track -> AudioGroup) foreach (AudioMixerGroup group in groups) { TrackInfo trackInfo = new TrackInfo(); trackInfo.Name = group.name; trackInfo.Group = group; trackInfo.TrackFader = null; _tracks[group.name] = trackInfo; } // Generate Pool for (int i = 0; i < _maxSounds; i++) { // Create GameObject and assigned AudioSource and Parent GameObject go = new GameObject("Pool Item"); AudioSource audioSource = go.AddComponent <AudioSource>(); go.transform.parent = transform; // Create and configure Pool Item AudioPoolItem poolItem = new AudioPoolItem(); poolItem.GameObject = go; poolItem.AudioSource = audioSource; poolItem.Transform = go.transform; poolItem.Playing = false; go.SetActive(false); _pool.Add(poolItem); } }
protected ulong ConfigurePoolObject(int poolIndex, string track, AudioClip clip, Vector3 position, float volume, float spatialBlend, float unimportance) { if (poolIndex < 0 || poolIndex >= pool.Count) { return(0); } AudioPoolItem poolItem = pool[poolIndex]; idGiver++; AudioSource source = poolItem.audioSource; source.clip = clip; source.volume = volume; source.spatialBlend = spatialBlend; // Asiign to requested audio group source.outputAudioMixerGroup = tracks[track].group; // Position source at requested position source.transform.position = position; // Enable gameobject and record that it is being played poolItem.playing = true; poolItem.unimportance = unimportance; poolItem.id = idGiver; poolItem.gameObject.SetActive(true); source.Play(); poolItem.coroutine = StopSoundDelayed(idGiver, source.clip.length); StartCoroutine(poolItem.coroutine); // Add this sound to our active pool with its unique id activePool[idGiver] = poolItem; return(idGiver); }
private void Awake() { DontDestroyOnLoad(gameObject); if (!mixer) { return; } AudioMixerGroup[] groups = mixer.FindMatchingGroups(string.Empty); foreach (AudioMixerGroup group in groups) { TrackInfo trackInfo = new TrackInfo(); trackInfo.name = group.name; trackInfo.group = group; trackInfo.trackFader = null; tracks[group.name] = trackInfo; } // Generate pool for (int i = 0; i < maxSounds; i++) { GameObject gameObject = new GameObject("Pool Item"); AudioSource audioSource = gameObject.AddComponent <AudioSource>(); gameObject.transform.parent = transform; AudioPoolItem poolItem = new AudioPoolItem(); poolItem.gameObject = gameObject; poolItem.audioSource = audioSource; poolItem.transform = gameObject.transform; poolItem.playing = false; gameObject.SetActive(false); pool.Add(poolItem); } }