72 lines
1.8 KiB
C#
72 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
public class ButtonSound : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler
|
|
{
|
|
[Header("Resources")]
|
|
public AudioSource audioObject;
|
|
|
|
[Header("Custom SFX")]
|
|
public AudioClip hoverSFX;
|
|
public AudioClip clickSFX;
|
|
|
|
[Header("Settings")]
|
|
public bool enableHoverSound = true;
|
|
public bool enableClickSound = true;
|
|
public bool checkForInteraction = true;
|
|
|
|
private Button sourceButton;
|
|
|
|
void OnEnable()
|
|
{
|
|
|
|
if (Application.isPlaying == true && audioObject == null)
|
|
{
|
|
try
|
|
{
|
|
audioObject = GameObject.Find("UI Audio").GetComponent<AudioSource>();
|
|
}
|
|
catch
|
|
{
|
|
Debug.Log("<b>[UI Element Sound]</b> No Audio Source found.", this);
|
|
}
|
|
}
|
|
|
|
if (checkForInteraction == true)
|
|
{
|
|
sourceButton = gameObject.GetComponent<Button>();
|
|
}
|
|
}
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (checkForInteraction == true && sourceButton != null && sourceButton.interactable == false)
|
|
return;
|
|
|
|
if (enableHoverSound == true)
|
|
{
|
|
if (hoverSFX != null)
|
|
{
|
|
audioObject.PlayOneShot(hoverSFX);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (checkForInteraction == true && sourceButton != null && sourceButton.interactable == false)
|
|
return;
|
|
|
|
if (enableClickSound == true)
|
|
{
|
|
if (clickSFX != null)
|
|
{
|
|
audioObject.PlayOneShot(clickSFX);
|
|
}
|
|
}
|
|
}
|
|
}
|