コード例 #1
0
        /// <summary>
        /// Plays the specified sound. Accepts names either relative to GameData or shorthand, e.g.
        /// "ScienceAlert/sounds/beep" or just "beep" assuming a sound with that name is loaded
        /// </summary>
        /// <param name="name"></param>
        /// <param name="volume"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        public bool Play(string name, float volume = 1f, float delay = 0f)
        {
            PlayableSound sound = null;

            if (sounds.ContainsKey(name))
            {
                sound = sounds[name];
            }
            else
            {
                //string located = sounds.Keys.ToList().Find(k => string.Equals(PlayableSound.GetShortName(k), sounds[k].shortName));
                string located = sounds.Keys.ToList().SingleOrDefault(k => string.Equals(PlayableSound.GetShortName(k), name));

                if (!string.IsNullOrEmpty(located) && sounds.ContainsKey(located))
                {
                    sound = sounds[located];
                }
            }

            if (sound == null)
            {
                Log.Error("AudioPlayer: Cannot play '{0}'!", name);
                return(false);
            }

            if (Time.realtimeSinceStartup - sound.nextPlayableTime > 0f)
            {
                if (source == null)
                {
                    SetSource(gameObject);
                }

                try
                {
                    source.PlayOneShot(sound.clip, Mathf.Clamp(volume, 0f, 1f));
                    sound.nextPlayableTime = Time.realtimeSinceStartup + delay;
                    Log.Debug("Played {0}, next available time {1}", sound.clip.name, sound.nextPlayableTime);

                    return(true);
                }
                catch (Exception e)
                {
                    Log.Error("AudioPlayer.Play exception while playing '{0}': {1}", name, e);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
        public bool Play(string name, float volume = 1f, float delay = 0f)
        {
            PlayableSound playableSound = null;

            if (sounds.ContainsKey(name))
            {
                playableSound = sounds[name];
            }
            else
            {
                string text = sounds.Keys.ToList <string>().SingleOrDefault((string k) => string.Equals(PlayableSound.GetShortName(k), name));
                if (!string.IsNullOrEmpty(text) && sounds.ContainsKey(text))
                {
                    playableSound = sounds[text];
                }
            }
            if (playableSound == null)
            {
                return(false);
            }

            if (!(Time.realtimeSinceStartup - playableSound.nextPlayableTime > 0f))
            {
                return(false);
            }

            if (source == null)
            {
                SetSource(gameObject);
            }
            try
            {
                source.PlayOneShot(playableSound.clip, Mathf.Clamp(volume, 0f, 1f));
                playableSound.nextPlayableTime = Time.realtimeSinceStartup + delay;
                bool result = true;
                return(result);
            }
            catch (System.Exception)
            {
                return(false);
            }
        }