Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created May 17, 2015 17:23
Show Gist options
  • Select an option

  • Save unitycoder/c4024061107c53bde3c9 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/c4024061107c53bde3c9 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist May 17, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions TrackMouseOverUI.cs
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    // http://forum.unity3d.com/threads/eventtrigger-move-issue.325103/#post-2114563

    public class TrackMouseOverUI: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    {
  2. unitycoder created this gist May 17, 2015.
    44 changes: 44 additions & 0 deletions TrackMouseOverUI.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;


    public class TrackMouseOverUI: 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.
    Debug.Log(localPos);

    yield return 0;
    }
    }
    else
    UnityEngine.Debug.LogWarning( "Could not find GraphicRaycaster and/or StandaloneInputModule" );
    }
    }