// Play an audio event. // // audioEventName is an audio event name from an audio bank // Event names are made of segments, ordered right to left in increasing order of specialisation // Eg. 'FOOTSTEP', 'GRAVEL, FOOTSTEP', 'WOLF, GRAVEL, FOOTSTEP' // The engine will strip the specialisations off, left to right, until it finds a match // Use MakeEvent to construct these. // // source is a GameObject to attach to - if null, uses a global emitter, useful for 2D sounds // // If successful, returns an AudioEmitter attached to a new GameObject public AudioEmitter Play(string audioEventName, GameObject source = null) { if (string.IsNullOrEmpty(audioEventName)) { return(null); } //If we're not attaching audio to another game object, assume this is a 2D sound and attach it to this manager if (source == null) { source = this.gameObject; } if (LogAllEvents) { Debug.Log("AudioManager: Trying event '" + audioEventName + "'" + (source != null ? (" (source = '" + source.name + "')") : "")); } // Look up an event from this trigger AudioEvent audioEvent = GetSoundForEvent(audioEventName); if (audioEvent != null && audioEvent.AudioClip != null) { var emitter = AudioEmitter.Create(source.transform, audioEvent); emitter.Play(); ActiveEmitters.AddLast(emitter); return(emitter); } else if (LogMissingEvents) { Debug.Log("$$$$Missing sound event '" + audioEventName + "' (source = '" + source.name + "')"); } return(null); }
public void Update() { // Update the music queue if (MusicQueue.Count > 0) { if (string.IsNullOrEmpty(CurrentMusicEvent)) { PlayMusic(MusicQueue.Dequeue()); } } //Delete any dead emitters LinkedListNode <AudioEmitter> currentNode = ActiveEmitters.First; while (currentNode != null) { if (currentNode.Value.Finished) { var node = currentNode; currentNode = currentNode.Next; GameObject.Destroy(node.Value.gameObject); ActiveEmitters.Remove(node); } else { currentNode = currentNode.Next; } } #if UNITY_EDITOR if (LogBankRefCounts) { foreach (KeyValuePair <string, int> kvp in BankReferenceCounts) { Debug.Log(kvp.Key.ToString() + ":" + kvp.Value.ToString()); } LogBankRefCounts = false; } #endif }