Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
VTDA's avatar
VTDA
Explorer
2 years ago

OnPointerExit & OnPointerEnter events unexpectedly fire after pressing & releasing the trigger

  • I have a simple script that plays an audio clip when the OnPointerEnter event fires and stops when the OnPointerExit event is fired
  • If I hover over and then off of a UI element, like a toggle or button, the audio plays / stops as expected
  • If I pull the controller trigger while hovering over UI and release, the OnPointerExit & OnPointerEnter events fire, even though I never moved the pointer from the UI
  • This only occurs when in VR, not in a desktop scene, and is feels like a bug

Unity 2022.3.20f1
Meta SDK v62

1 Reply

  • This is what I did in lieu of an official fix by Meta to satisfy my specific use case (to predictably play audio on hover events):

    using UnityEngine;
    using Oculus.Interaction;
    
    public class PlayAudioOnHoverMETA : MonoBehaviour
    {
        [SerializeField] private AudioSource audioSource;
        private bool isPointerOver = false;
        
        void Start()
        {
            PointableCanvasModule.WhenSelectableHovered += OnSelectableHovered;
            PointableCanvasModule.WhenSelectableUnhovered += OnSelectableUnhovered;
        }
    
        void OnDestroy()
        {
            PointableCanvasModule.WhenSelectableHovered -= OnSelectableHovered;
            PointableCanvasModule.WhenSelectableUnhovered -= OnSelectableUnhovered;
        }
    
        private void OnSelectableHovered(PointableCanvasEventArgs args)
        {
            if (args.Hovered == this.gameObject && audioSource != null && !audioSource.isPlaying && !isPointerOver)
            {
                audioSource.Play();
                isPointerOver = true;
                //Debug.Log("Play Audio Clip on Enter\n");
            }
        }
    
        private void OnSelectableUnhovered(PointableCanvasEventArgs args)
        {
            if (audioSource != null)
            {
                audioSource.Stop();
                isPointerOver = false;
                //Debug.Log("Stop Audio Clip on Exit\n");
            }
        }
    }