Encounter / Assets / Scripts / AudioHandling / ButtonInteractions.cs
ButtonInteractions.cs
Raw
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// Handles secondary button interactions such as sound
/// </summary>
public class ButtonInteractions : MonoBehaviour, IPointerEnterHandler, IPointerClickHandler
{
    [Header("Sounds")]
    [SerializeField] private AudioClip hoverSound;              // The sound to be made when a button is being hovered over
    [SerializeField] private AudioClip pressedSound;            // The sound to be made when a button is being clicked on

    /// <summary>
    /// Checks the condition of the button component to make sure it is able to play sound effects when enabled
    /// Makes sure the button is not disabled before playing sound effects
    /// </summary>
    private bool IsApplicable
    {
        get => GetComponent<Button>().interactable;
    }
    /// <summary>
    /// Plays the hovering sound effect when the object is being hovered over
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (IsApplicable && hoverSound != null)
            SoundManager.Instance.PlaySoundFX(hoverSound);
    }
    /// <summary>
    /// Plays the pressed sound effect when the object is clicked on
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerClick(PointerEventData eventData)
    {
        if (IsApplicable && pressedSound != null)
            SoundManager.Instance.PlaySoundFX(pressedSound);
    }
}