using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class ButtonSound : MonoBehaviour
{
public AudioClip sound;
private Button button { get { return GetComponent<Button>(); } }
private AudioSource source { get { return GetComponent<AudioSource>(); } }
// Start is called before the first frame update
void Start()
{
gameObject.AddComponent<AudioSource>();
source.clip = sound;
source.playOnAwake = false;
button.onClick.AddListener(() => PlaySound());
}
// Update is called once per frame
void PlaySound()
{
source.Play();
}
}
In Audio Source Loop is true;