// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2021-01-22 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace G { /// /// 声音 /// public class KSoundFx : MonoBehaviour { #region Field public string[] soundNames; public int soundGroup = SoundProxy.FX_LAYER_INDEX; public bool playAwake; public bool button; #endregion #region Method public void Play() { if (soundNames != null && soundNames.Length > 0) { Play(soundNames.Length == 1 ? soundNames[0] : soundNames[Random.Range(0, soundNames.Length)]); } } private void Play(string clipName) { if (!string.IsNullOrEmpty(clipName)) { if (soundGroup == SoundProxy.BG_LAYER_INDEX) { SoundProxy.PlayBgAsync(clipName); } else { SoundProxy.PlayFxAsync(clipName); } } } #endregion #region Unity private void OnEnable() { if (playAwake) { Play(); } } // Use this for initialization private void Start() { if (button) { var selectable = GetComponent(); if (selectable is Button btn) { btn.onClick.AddListener(this.Play); return; } if (selectable is Toggle tg) { tg.onValueChanged.AddListener((status) => { if (status) this.Play(); }); return; } } } #endregion } }