示例#1
0
        /// <summary>
        /// Play a clip using PlayableAudioDefinition param
        /// </summary>
        /// <param name="audioDefinition">Define how the clip must be played</param>
        public void PlayClip(PlayableAudioDefinition audioDefinition)
        {
            AudioClipSettings clipSettings;
            AudioSettings3D   audioSettings3D = null;

            //If 3d audio the settings class is created and populated with defined info in audioDefinition
            if (audioDefinition.is3Daudio)
            {
                audioSettings3D = new AudioSettings3D
                {
                    position     = transform.position,
                    rollofMode   = audioDefinition.rollofMode,
                    spatialBlend = audioDefinition.spatialBlend,
                    minDistance  = audioDefinition.minDistance,
                    maxDistance  = audioDefinition.maxDistance
                };
            }
            //Calculate pitch (random or not)
            float pitch = audioDefinition.hasRandomPitch ? UnityEngine.Random.Range(audioDefinition.minRandomPitch, audioDefinition.maxRandomPitch) : 1;

            //Create clipSettings
            clipSettings = new AudioClipSettings(audioDefinition.audioClip, pitch, audioDefinition.loop, audioDefinition.volume, 0);
            string audioId = audioDefinition.audioClip.name;

            //IF THIS INSTANCE CAN PLAY ONLY ONCE AUDIO X TIME I WILL DISABLE CURRENT PLAYNG AUDIO IF EXIST
            if (audioDefinition.onceAtTime && currentPlayngAudio != null && currentPlayngAudio.isPlaying)
            {
                currentPlayngAudio.Stop();
                currentPlayngAudio.gameObject.SetActive(false);
            }
            //Play Audio
            currentPlayngAudio = AudioManager.PlayAudio(audioDefinition.audioClip.name, clipSettings, audioSettings3D);
        }
示例#2
0
        /// <summary>
        /// Get audio from pool or add it to pool and then play
        /// </summary>
        /// <param name="audioSourceId"></param>
        /// <param name="clip"></param>
        /// <param name="pitch"></param>
        /// <param name="loop"></param>
        /// <param name="volume"></param>
        /// <param name="delay"></param>
        /// <param name="overflow"></param>
        /// <returns></returns>
        public static AudioSource PlayAudio(string audioSourceId, AudioClip clip, float pitch = 1, bool loop = false, float volume = 1, float delay = 0, bool overflow = true)
        {
            AudioClipSettings audioSettings = new AudioClipSettings(clip, delay, loop, volume, pitch, overflow);

            return(PlayAudio(audioSourceId, audioSettings));
        }
示例#3
0
        /// <summary>
        /// Get audio from pool or add it to pool and then play
        /// </summary>
        /// <param name="audioSourceId">Best pratice is to use clip.name</param>
        /// <param name="settings">Audio settings</param>
        /// <param name="settings3d"></param>
        /// <returns></returns>
        public static AudioSource PlayAudio(string audioSourceId, AudioClipSettings settings, AudioSettings3D settings3d = null)
        {
            if (AudioManager.Instance == null || settings.clip == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(audioSourceId)) //IF id is empty i will use clip.name
            {
                audioSourceId = settings.clip.name;
            }

            AudioSource aSource;

            //If audio is not pooled it will be pooled else will be use pooled one
            if (!AudioManager.Instance.audioSourcePool.ContainsKey(audioSourceId))
            {
                aSource = AddAudioToPool(audioSourceId);
            }
            else
            {
                aSource = AudioManager.Instance.getPooledAudio(audioSourceId);
            }
            // stop audio as first thing to avoid strange problems
            aSource.Stop();

            //Set AudioClipSettings in audiosource component
            float realVolume = (settings.volume * AudioManager.Instance.globalVolume); //Calculate volume

            aSource.clip        = settings.clip;                                       // Define the clips
            aSource.playOnAwake = false;                                               // Disable play on awake to customize play time (the play function is called at the end of procedure)
            aSource.volume      = realVolume;                                          // Apply volume
            aSource.pitch       = settings.pitch;                                      //Aplly pitch
            aSource.loop        = settings.loop;                                       //Set loop
            aSource.mute        = settings.mute;                                       // Set Mute

            aSource.bypassEffects         = settings.bypassEffects;
            aSource.bypassListenerEffects = settings.bypassListenerEffects;
            aSource.bypassReverbZones     = settings.bypassReverbZones;
            aSource.priority      = settings.priority;
            aSource.panStereo     = settings.stereoPan;
            aSource.spatialBlend  = settings.spatialBled;
            aSource.reverbZoneMix = settings.reverbZoneMix;
            aSource.spatialBlend  = 0; //This will be overrided if settings3d is not null

            //Enable audio object
            aSource.gameObject.SetActive(true);

            //If settings3d is set they will be applyed over there
            if (settings3d != null)
            {
                aSource.rolloffMode  = settings3d.rollofMode;
                aSource.spatialBlend = settings3d.spatialBlend;
                aSource.dopplerLevel = settings3d.dopplerLevel;
                aSource.spread       = settings3d.spread;
                aSource.minDistance  = settings3d.minDistance;
                aSource.maxDistance  = settings3d.maxDistance;
                aSource.gameObject.transform.position = settings3d.position;
                aSource.Play();
            }
            else if (settings.delay > 0)
            {
                aSource.PlayDelayed(settings.delay); //Play audio after delay
            }
            else
            {
                aSource.Play(); //Play audio without delay
            }
            return(aSource);
        }