unity ui in use onmouseover

unity ui mouse moved into or out of the trigger and 2d, 3d of different, 2d, 3d objects using onmouseover, ui using OnPointerEnter. We need to implement the following two interfaces.

public class TrackMouse: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    // Called when the pointer enters our GUI component.
    // Start tracking the mouse
    public void OnPointerEnter( PointerEventData eventData )
    {
        StartCoroutine( "TrackPointer" );          
    }
 
    // Called when the pointer exits our GUI component.
    // Stop tracking the mouse
    public void OnPointerExit( PointerEventData eventData )
    {
        StopCoroutine( "TrackPointer" );
    }
 
    IEnumerator TrackPointer()
    {
        var ray = GetComponentInParent<GraphicRaycaster>();
        var input = FindObjectOfType<StandaloneInputModule>();
 
        if( ray != null && input != null )
        {
            while( Application.isPlaying )
            {                  
                Vector2 localPos; // Mouse position  
                RectTransformUtility.ScreenPointToLocalPointInRectangle( transform as RectTransform, Input.mousePosition, ray.eventCamera, out localPos );
                     
                // local pos is the mouse position.
                     
                yield return 0;
            }      
        }
        else
            UnityEngine.Debug.LogWarning( "Could not find GraphicRaycaster and/or StandaloneInputModule" );      
    }
}
 

  From the original unity official forum, the details can be viewed https://forum.unity.com/threads/problem-with-onmousedown-onmouseover-in-ui.326096/

 

Guess you like

Origin www.cnblogs.com/zhoushiya/p/12208548.html