Exemplo n.º 1
0
        /// <summary>
        /// Plays a sound differently based on the selected play method
        /// </summary>
        /// <param name="sfx"></param>
        /// <param name="position"></param>
        protected virtual void PlaySound(AudioClip sfx, Vector3 position, float intensity)
        {
            float volume = Random.Range(MinVolume, MaxVolume);

            if (!Timing.ConstantIntensity)
            {
                volume = volume * intensity;
            }

            float pitch = Random.Range(MinPitch, MaxPitch);

            int timeSamples = NormalPlayDirection ? 0 : sfx.samples - 1;

            if (!NormalPlayDirection)
            {
                pitch = -pitch;
            }

            if (PlayMethod == PlayMethods.Event)
            {
                MMSfxEvent.Trigger(sfx, SfxAudioMixerGroup, volume, pitch);
                return;
            }

            if (PlayMethod == PlayMethods.OnDemand)
            {
                // we create a temporary game object to host our audio source
                GameObject temporaryAudioHost = new GameObject("TempAudio");
                SceneManager.MoveGameObjectToScene(temporaryAudioHost.gameObject, this.gameObject.scene);
                // we set the temp audio's position
                temporaryAudioHost.transform.position = position;
                // we add an audio source to that host
                AudioSource audioSource = temporaryAudioHost.AddComponent <AudioSource>() as AudioSource;
                PlayAudioSource(audioSource, sfx, volume, pitch, timeSamples, SfxAudioMixerGroup);
                // we destroy the host after the clip has played
                Destroy(temporaryAudioHost, sfx.length);
            }

            if (PlayMethod == PlayMethods.Cached)
            {
                // we set that audio source clip to the one in paramaters
                PlayAudioSource(_cachedAudioSource, sfx, volume, pitch, timeSamples, SfxAudioMixerGroup);
            }

            if (PlayMethod == PlayMethods.Pool)
            {
                _tempAudioSource = GetAudioSourceFromPool();
                if (_tempAudioSource != null)
                {
                    PlayAudioSource(_tempAudioSource, sfx, volume, pitch, timeSamples, SfxAudioMixerGroup);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Plays a sound differently based on the selected play method
        /// </summary>
        /// <param name="sfx"></param>
        /// <param name="position"></param>
        protected virtual void PlaySound(AudioClip sfx, Vector3 position)
        {
            if (PlayMethod == PlayMethods.Event)
            {
                float volume = Random.Range(MinVolume, MaxVolume);
                float pitch  = Random.Range(MinPitch, MaxPitch);
                MMSfxEvent.Trigger(sfx, SfxAudioMixerGroup, volume, pitch);
                return;
            }

            if (PlayMethod == PlayMethods.OnDemand)
            {
                float volume = Random.Range(MinVolume, MaxVolume);
                float pitch  = Random.Range(MinPitch, MaxPitch);

                // we create a temporary game object to host our audio source
                GameObject temporaryAudioHost = new GameObject("TempAudio");
                // we set the temp audio's position
                temporaryAudioHost.transform.position = position;
                // we add an audio source to that host
                AudioSource audioSource = temporaryAudioHost.AddComponent <AudioSource>() as AudioSource;
                // we set that audio source clip to the one in paramaters
                audioSource.clip = sfx;
                // we set the audio source volume to the one in parameters
                audioSource.volume = volume;
                audioSource.pitch  = pitch;
                // we set our loop setting
                audioSource.loop         = false;
                audioSource.spatialBlend = SpatialBlend;
                // we start playing the sound
                audioSource.Play();
                // we destroy the host after the clip has played
                Destroy(temporaryAudioHost, sfx.length);
            }

            if (PlayMethod == PlayMethods.Cached)
            {
                float volume = Random.Range(MinVolume, MaxVolume);
                float pitch  = Random.Range(MinPitch, MaxPitch);
                // we set that audio source clip to the one in paramaters
                _cachedAudioSource.clip = sfx;
                // we set the audio source volume to the one in parameters
                _cachedAudioSource.volume = volume;
                _cachedAudioSource.pitch  = pitch;
                // we set our loop setting
                _cachedAudioSource.loop = false;
                // we start playing the sound
                _cachedAudioSource.Play();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Plays a sound differently based on the selected play method
        /// </summary>
        /// <param name="sfx"></param>
        /// <param name="position"></param>
        protected virtual void PlaySound(AudioClip sfx, Vector3 position)
        {
            float volume = Random.Range(MinVolume, MaxVolume);
            float pitch  = Random.Range(MinPitch, MaxPitch);

            if (PlayMethod == PlayMethods.Event)
            {
                MMSfxEvent.Trigger(sfx, SfxAudioMixerGroup, volume, pitch);
                return;
            }

            if (PlayMethod == PlayMethods.OnDemand)
            {
                // we create a temporary game object to host our audio source
                GameObject temporaryAudioHost = new GameObject("TempAudio");
                // we set the temp audio's position
                temporaryAudioHost.transform.position = position;
                // we add an audio source to that host
                AudioSource audioSource = temporaryAudioHost.AddComponent <AudioSource>() as AudioSource;
                PlayAudioSource(audioSource, sfx, volume, pitch, SfxAudioMixerGroup);
                // we destroy the host after the clip has played
                Destroy(temporaryAudioHost, sfx.length);
            }

            if (PlayMethod == PlayMethods.Cached)
            {
                // we set that audio source clip to the one in paramaters
                PlayAudioSource(_cachedAudioSource, sfx, volume, pitch, SfxAudioMixerGroup);
            }

            if (PlayMethod == PlayMethods.Pool)
            {
                _tempAudioSource = GetAudioSourceFromPool();
                if (_tempAudioSource != null)
                {
                    PlayAudioSource(_tempAudioSource, sfx, volume, pitch, SfxAudioMixerGroup);
                }
            }
        }