DET24-AtomCraft/AtomURP-main/Assets/Scripts/ButtonPlaySound.cs
2024-03-18 17:17:19 +01:00

40 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class YourButtonScript : MonoBehaviour
{
public AudioSource audioSource;
private bool isPlaying = false;
void Start()
{
// Get the AudioSource component from the GameObject
audioSource = GetComponent<AudioSource>();
}
public void PlaySound()
{
// Check if audioSource and clip are assigned and not already playing
if (audioSource != null && audioSource.clip != null && !isPlaying)
{
// Play the sound clip
audioSource.Play();
isPlaying = true;
// Invoke a method to reset isPlaying after the clip has finished playing
Invoke("ResetIsPlaying", audioSource.clip.length);
}
else
{
Debug.LogWarning("AudioSource or AudioClip not assigned or already playing.");
}
}
private void ResetIsPlaying()
{
isPlaying = false;
}
}