Ejemplo n.º 1
0
        ///// <summary>
        ///// Pauses all sounds
        ///// </summary>
        //public void PauseAllSounds()
        //{
        //    TraverseAllSources(GetComponents(typeof(AudioSource)), PauseHandler);
        //}

        ///// <summary>
        ///// Plays all sounds
        ///// </summary>
        //public void PlayAllSounds()
        //{
        //    TraverseAllSources(GetComponents(typeof(AudioSource)), PlayHandler);
        //}

        #endregion

        #region Private methods

        /// <summary>
        /// Looks for a token
        /// </summary>
        /// <param name="id">Token ID</param>
        /// <returns>Audio token</returns>
        private AudioToken PickToken(string id)
        {
            Component[] tokens = GetComponents(typeof(AudioToken));

            if (tokens.Length == 0)
            {
                //throw new AudioPlayerException(AudioPlayerException.NoTokensFound);
                Debug.LogWarning(AudioPlayerException.NoTokensFound);
                return(null);
            }

            AudioToken token = null;

            foreach (Component t in tokens)
            {
                AudioToken tok = (AudioToken)t;

                if (tok.Id == id)
                {
                    token = tok;
                    break;
                }
            }

            return(token);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Searches the token with the specified ID attached to the same object as this script
        /// Plays it with an audio source attached to the same object as this script
        /// </summary>
        /// <param name="tokenId"></param>
        /// <param name="options"></param>
        public void PlaySound(string tokenId, params AudioOption[] options)
        {
            if (!SoundEnabled)
            {
                return;
            }

            if (PlayAtMainCamera)
            {
                ChangePosition();
            }

            AudioToken token = PickToken(tokenId);

            if (null == token)
            {
                Debug.LogWarning(string.Format(AudioPlayerException.TokenNotFound, tokenId));
                return;
            }

            if (null == token.AudioClip)
            {
                Debug.LogWarning(string.Format(AudioPlayerException.TokenAudioClipNotFound, tokenId));
                return;
            }

            token.ApplyOptions(options);

            //Debug.Log("Token found: " + token);

            AudioSource audioSource = PickSource();

            audioSource.clip   = token.AudioClip;
            audioSource.volume = token.Volume * Volume;
            audioSource.pitch  = Random.Range(token.Pitch * Pitch - token.PitchRandomness - PitchRandomness, token.Pitch + token.PitchRandomness + PitchRandomness);
            audioSource.loop   = token.Loop;
            //audioSource.minVolume = token.MinDistance;
            //audioSource.maxVolume = token.MaxDistance;
            //audioSource.rolloffFactor = token.RolloffMode;
            audioSource.minDistance = token.MinDistance;
            audioSource.maxDistance = token.MaxDistance;
            audioSource.rolloffMode = token.RolloffMode;
            audioSource.Play();
        }