Example #1
0
    public void PlaySound(GameSoundType soundType)
    {
        if (config == null)
        {
            Debug.Log("You must setup your sound config to play sounds!");
        }
        GameSound gameSound = config.Sounds.Find(sound => sound.Type == soundType);

        if (gameSound == null)
        {
            Debug.Log($"Couldn't find a sound for {soundType}!");
        }
        AudioClip clip = gameSound.Clip;

        if (gameSound.Clips != null && gameSound.Clips.Count > 0)
        {
            clip = gameSound.Clips[Random.Range(0, gameSound.Clips.Count)];
        }
        PlayClip(clip, gameSound.Volume);

        /*
         * GameSoundSource source = soundSources.Find(sound => sound.Clip == clip);
         * if (source == null)
         * {
         *  source = Prefabs.Get<GameSoundSource>();
         *  source.transform.SetParent(transform);
         *  source.Init(clip);
         *  soundSources.Add(source);
         * }
         * source.Play();*/
    }
Example #2
0
        private void RegisterAudioSource(GameSoundType type, AudioSource audioSource)
        {
            AudioGlobalControl audioGlobalControl = new AudioGlobalControl();

            audioGlobalControl.Audio = audioSource;
            audioGlobalControl.Type  = type;

            switch (type)
            {
            case GameSoundType.Background:
                audioSource.outputAudioMixerGroup = GameMainAudioMixerGroupBackgroundMusic;
                break;

            case GameSoundType.BallEffect:
                audioSource.outputAudioMixerGroup = GameMainAudioMixerGroupBallEffect;
                break;

            case GameSoundType.ModulEffect:
                audioSource.outputAudioMixerGroup = GameMainAudioMixerGroupModulEffect;
                break;

            case GameSoundType.Normal:
                audioSource.outputAudioMixerGroup = GameMainAudioMixerGroupMaster;
                break;

            case GameSoundType.UI:
                audioSource.outputAudioMixerGroup = GameUIAudioMixerGroupMaster;
                break;
            }


            audios.Add(audioGlobalControl);
        }
Example #3
0
 /// <summary>
 /// 注册已有 SoundPlayer
 /// </summary>
 /// <param name="type">ui/normal/background</param>
 /// <param name="audioSource">AudioSource</param>
 /// <returns></returns>
 public AudioSource RegisterSoundPlayer(GameSoundType type, AudioSource audioSource)
 {
     if (!IsSoundPlayerRegistered(audioSource))
     {
         RegisterAudioSource(type, audioSource);
     }
     else
     {
         GameErrorManager.LastError = GameError.AlredayRegistered;
     }
     return(audioSource);
 }
Example #4
0
        /// <summary>
        /// 注册 SoundPlayer
        /// </summary>
        /// <param name="audioClip">音频源文件</param>
        /// <returns></returns>
        public AudioSource RegisterSoundPlayer(GameSoundType type, AudioClip audioClip, bool playOnAwake = false, bool activeStart = true, string name = "")
        {
            AudioSource audioSource = Instantiate(audioSourcePrefab, gameObject.transform).GetComponent <AudioSource>();

            audioSource.clip            = audioClip;
            audioSource.playOnAwake     = playOnAwake;
            audioSource.gameObject.name = "AudioSource_" + type + "_" + (name == "" ? GamePathManager.GetFileNameWithoutExt(audioClip.name) : name);

            if (!activeStart)
            {
                audioSource.gameObject.SetActive(false);
            }

            RegisterAudioSource(type, audioSource);
            return(audioSource);
        }
Example #5
0
        /// <summary>
        /// 快速播放一个短声音
        /// </summary>
        /// <param name="soundName">声音资源字符串</param>
        /// <param name="type">声音类型</param>
        /// <returns></returns>
        public bool PlayFastVoice(string soundName, GameSoundType type)
        {
            string      key   = soundName + "@" + type;
            AudioSource cache = null;

            if (fastPlayVoices.TryGetValue(key, out cache))
            {
                cache.Play();
                return(true);
            }
            AudioClip audioClip = LoadAudioResource(soundName);

            if (audioClip == null)
            {
                return(false);
            }

            cache = RegisterSoundPlayer(type, audioClip, false, true, key);
            cache.Play();
            return(true);
        }
Example #6
0
 public static void StopLoop(GameSoundType type)
 {
     Sounds[type].loop.Stop();
 }
Example #7
0
 public static void LoopSound(GameSoundType type, float volume)
 {
     Sounds[type].Loop(volume);
 }
Example #8
0
 public static void LoopSound(GameSoundType type)
 {
     Sounds[type].Loop();
 }
Example #9
0
 public static void PlaySound(GameSoundType type)
 {
     Sounds[type].Play();
 }