-2
\$\begingroup\$

How to play a sound when moving a cursor on a button ?

\$\endgroup\$
  • \$\begingroup\$ -1 "does not show any research effort" What did you try? Do you have an issue with the mouse over detection? Do you have an issue with playing a sound? Do you want to play a one-shot sound or do you want the sound to repeat? \$\endgroup\$ – Alexandre Vaillancourt Nov 4 '17 at 22:51
2
\$\begingroup\$

Unity has interfaces you can implement in order to detect when the pointer enters / leaves your UI element :

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;// Required when using Event data.

public class ExampleClass : MonoBehaviour, IPointerEnterHandler // required interface when using the OnPointerEnter method.
{
    public AudioClip AudioClip ;
    private AudioSource audioSource ;

    //Do this when the cursor enters the rect area of this selectable UI object.
    public void OnPointerEnter(PointerEventData eventData)
    {
        if( audioSource == null )
            audioSource = GetComponent<AudioSource>();
        if( audioSource == null )
            audioSource = gameObject.AddComponent<AudioSource>();

        audioSource.PlayOneShot( AudioClip ) ;
    }
}
\$\endgroup\$
-1
\$\begingroup\$

Maybe this could help you. Add such an event to your button in the same manner as you add an OnClick() event. Let me know if it was helpful!

\$\endgroup\$
  • \$\begingroup\$ -1 How would they use the event; could you provide a code example? How is this linked to the fact that the OP wants to play a sound? \$\endgroup\$ – Alexandre Vaillancourt Nov 4 '17 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.