chunk 1: core gameplay scripts scenes runtime assets
This commit is contained in:
40
Assets/Scripts/Button Click/ButtonClickSound.cs
Normal file
40
Assets/Scripts/Button Click/ButtonClickSound.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user