// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-12-15 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** #if SDK_WECHAT_WASM #define PLAY_PLATFORM_SOUND #endif using System.Collections; using System.Collections.Generic; using UnityEngine; namespace G { /// /// 角色声音类(支持3d) /// public class Cha_Sound : MonoBehaviour { #region Field /// /// /// public string[] sndYells = new string[3]; public string[] sndSkillYells = new string[2]; public string[] sndBlades; public string sndFootstep; public string sndGetItem; public string sndBoom; public string sndDodge; public string sndSwing; public string sndSkillStart; public string sndRiseAttack; public string sndBlock; public string sndBlockBreak; public string sndDied; private AudioSource _audioSource; private readonly Dictionary _audioClips = new Dictionary(); #endregion #region Method /// /// /// public void PlaySoundDodge() { PlaySound(sndDodge); } public void PlaySoundAttack() { int si = Random.Range(0, 5); if (si < 2) { PlaySound(sndYells[si]); } } public void PlayYell(int index) { PlaySound(sndYells[index]); } public void PlaySkillStart() { PlaySoundOneShot(sndSkillStart); } public void PlayBoom() { PlaySound(sndBoom); } public void PlaySound(string name) { #if !PLAY_PLATFORM_SOUND if (_audioClips.TryGetValue(name, out var audioClip)) PlaySound(audioClip); #else SoundProxy.PlayFxAsync(name); #endif } private void PlaySound(AudioClip clip) { if (_audioSource && clip) { _audioSource.clip = clip; _audioSource.Play(); } } public void PlaySoundOneShot(string name) { #if !PLAY_PLATFORM_SOUND if (_audioClips.TryGetValue(name, out var audioClip)) PlaySoundOneShot(audioClip); #else SoundProxy.PlayFxAsync(name); #endif } /// /// /// /// private void PlaySoundOneShot(AudioClip clip) { if (_audioSource && clip) { _audioSource.PlayOneShot(clip); } } #endregion #region Unity // Use this for initialization private void Awake() { _audioSource = GetComponent(); SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX); } private IEnumerator Start() { #if !PLAY_PLATFORM_SOUND var handle = AssetProxy.Instance.TryGetTemporaryAssetsAsync("cha_sound"); yield return handle; foreach (var clip in handle.Result) { _audioClips.Add(clip.name, clip); } #else yield return null; #endif } #endregion } }