forked from nope5166/AtomCraft-2.28
40 lines
1.0 KiB
C#
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;
|
|
}
|
|
}
|