Ejemplo n.º 1
0
        private void Setup()
        {
            // Create an audio pool for each audio channel
            AudioMixerData[] allMixers = AudioSettings.AllMixers;
            audioPools = new AudioMixerPool[allMixers.Length];

            // Setup an audio pool for every audio channel
            for (int i = 0; i < audioPools.Length; i++)
            {
                audioPools[i] = new AudioMixerPool(i, gameObject);
            }
        }
Ejemplo n.º 2
0
        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);
        }