2025-05-18 01:04:31 +08:00

93 lines
1.8 KiB
C#

// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2021-01-22
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "KSoundFx" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace G
{
/// <summary>
/// 声音
/// </summary>
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<Selectable>();
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
}
}