private static AudioSource PlayInternal(AudioClip clip, AudioMixerIndex index, int sourceIndex, bool looping, string methodSuffix) { // If index is null then set it to default if (index == null) { index = AudioMixerIndex.Default; } // If index is invalid then throw an exception if (index.Index < 0 || index.Index >= Instance.audioPools.Length) { throw IndexOutOfRangeException(index); } // Get the audio pool and identified method AudioMixerPool pool = Instance.audioPools[index.Index]; string methodName = $"Play{methodSuffix}"; MethodInfo playMethod = pool.GetType().GetMethod($"Play{methodSuffix}"); // If reflection did not find the method then throw an internal error if (playMethod == null) { throw new Exception($"INTERNAL ERROR: " + $"no method defined in type '{nameof(AudioMixerPool)}' " + $"with the name '{methodName}'"); } // Get the result of the method object result = playMethod.Invoke(pool, new object[] { clip, sourceIndex, looping }); // Check to make sure the object is not null if (result is null) { throw new Exception("INTERNAL ERROR: " + $"The return value of the method '{methodName}' " + $"for the type '{nameof(AudioMixerPool)}' " + $"is not permitted to be null"); } // Check to make sure the result is an audio source if (!(result is AudioSource)) { throw new InvalidCastException($"INTERNAL ERROR: " + $"The return value of the method '{methodName}' " + $"for the type '{nameof(AudioMixerPool)}' must be of type " + $"'{nameof(AudioSource)}', not type '{result.GetType().Name}'"); } return(result as AudioSource); }
public static AudioMixerData GetMixer(AudioMixerIndex index) { AudioMixerData[] mixers = AllMixers; // If index is in range then return the correct mixer if (index.Index >= 0 && index.Index < mixers.Length) { return(mixers[index.Index]); } // If index is out of range then throw an exception else { throw new IndexOutOfRangeException( $"No audio mixer data associated with index '{index.Index}'. " + $"Total mixers: {mixers.Length}"); } }
public AudioMixerPool(int index, GameObject parent) { // Setup the index for the pool this.index = new AudioMixerIndex(index); // Create a new game object with the same name as the mixer gameObject = new GameObject($"Mixer {index}: {this.index.Data.Mixer}"); gameObject.transform.SetParent(parent.transform); // Get a list of all channels AudioChannel[] channels = this.index.Data.AllChannels; pools = new AudioChannelPool[channels.Length]; // Create a pool for each channel for (int i = 0; i < channels.Length; i++) { pools[i] = new AudioChannelPool(index, i, gameObject); } }
public AudioChannelIndex(AudioMixerIndex mixerIndex, int channelIndex) { this.mixerIndex = mixerIndex; this.channelIndex = channelIndex; }
// SFX channel public static AudioSource PlaySFX(AudioClip clip, AudioMixerIndex index = null, int sourceIndex = -1, bool looping = false) { return(PlayInternal(clip, index, sourceIndex, looping, "SFX")); }
private static IndexOutOfRangeException IndexOutOfRangeException(AudioMixerIndex index) { return(new IndexOutOfRangeException( $"No audio pool associated with {index.Index}. " + $"Total pools: {Instance.audioPools}")); }