public AudioChannelPool(int mixerIndex, int channelIndex, GameObject parent) { // Set this template channel index = (mixerIndex, channelIndex); // Get the channel referenced by the index AudioChannel channel = AudioSettings.GetChannel(index); // Create an object under my transform gameObject = new GameObject($"Output {channelIndex}: '{channel.Output}'"); gameObject.transform.SetParent(parent.transform); // Initialize the array pool = new AudioSource[channel.AudioSourceCount]; // Create an audio source for each element in the array for (int i = 0; i < pool.Length; i++) { // Create an object for the source and set its parent GameObject sourceObject = new GameObject($"Source {i}"); sourceObject.transform.SetParent(gameObject.transform); // Add the audio source and set it up pool[i] = sourceObject.AddComponent <AudioSource>(); pool[i].outputAudioMixerGroup = channel.Output; pool[i].playOnAwake = false; } }
// Change this to not throw and instead use an "out" parameter public static bool GetVolume(AudioChannelIndex index, out float volume) { bool result = GetDecibels(index, out volume); volume = DecibelsToVolume(volume); return(result); }
// Change this to return true or false public static void ResetDecibels(AudioChannelIndex index) { AudioMixer mixer = index.MixerIndex.Data.Mixer; bool success = mixer.ClearFloat(GetVolumeParameterName(index)); if (!success) { Debug.LogWarning(GetMissingParameterWarning(index)); } }
// Change this to not throw and instead use an "out" parameter public static bool GetDecibels(AudioChannelIndex index, out float decibels) { AudioMixer mixer = index.MixerIndex.Data.Mixer; bool result = mixer.GetFloat(GetVolumeParameterName(index), out decibels); // If there was no volume then log a warning if (!result) { Debug.LogWarning(GetMissingParameterWarning(index)); } return(result); }
// Change this to return true or false public static bool SetVolume(AudioChannelIndex index, float volume) { AudioMixer mixer = index.MixerIndex.Data.Mixer; bool success = mixer.SetFloat( GetVolumeParameterName(index), VolumeToDecibels(volume)); if (!success) { Debug.LogWarning(GetMissingParameterWarning(index)); } return(success); }
// Arbitrary channel public static AudioSource PlayFromChannel(AudioClip clip, AudioChannelIndex index, int sourceIndex = -1, bool looping = false) { // If index is null then throw an exception if (index == null) { throw new ArgumentNullException(nameof(index)); } // If the number is invalid then throw an exception if (index.MixerIndex.Index < 0 || index.MixerIndex.Index >= Instance.audioPools.Length) { throw IndexOutOfRangeException(index.MixerIndex); } // Play from the channel on the correct audio pool return(Instance.audioPools[index.MixerIndex.Index].PlayFromChannel(clip, index.ChannelIndex, sourceIndex, looping)); }
public static AudioChannel GetChannel(AudioChannelIndex index) { AudioMixerData mixer = GetMixer(index.MixerIndex); return(mixer.GetChannel(index.ChannelIndex)); }
public static string GetVolumeParameterName(AudioChannelIndex index) { return($"{index.Channel.Output.name}Volume"); }
public static string GetMissingParameterWarning(AudioChannelIndex index) { return($"Expected audio mixer '{index.MixerIndex.Data.Mixer}' " + $"to have exposed parameter with the name '{GetVolumeParameterName(index)}', " + $"but no such parameter could be found."); }