Files

41 lines
1.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class ButtonClickSound : MonoBehaviour
{
public AudioClip clickSound;
private Button button;
private AudioSource audioSource;
private void Awake()
{
button = GetComponent<Button>();
audioSource = GetComponent<AudioSource>();
// Create an AudioSource component if one doesn't already exist
if (audioSource == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
// Set the AudioClip to play when the button is clicked
audioSource.clip = clickSound;
// Make sure the AudioSource doesn't play automatically
audioSource.playOnAwake = false;
}
private void Start()
{
// Attach the button click event to the PlayClickSound() method
button.onClick.AddListener(PlayClickSound);
}
public void PlayClickSound()
{
// Play the click sound using the Manager
GameManager.Instance.PlaySound(clickSound);
}
}