Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Created January 29, 2019 06:20
Show Gist options
  • Save HassakuTb/2db32099edcec55e9f7d17d25f59733e to your computer and use it in GitHub Desktop.
Save HassakuTb/2db32099edcec55e9f7d17d25f59733e to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Graphics))]
public class MouseOverDetector : MonoBehaviour{
[SerializeField] private GraphicRaycaster raycaster; // Required
[SerializeField] private EventSystem eventSystem; // Required
/// <summary>
/// マウスカーソルがこのUI上に存在するか
/// </summary>
/// <returns></returns>
public bool IsMouseOver()
{
List<RaycastResult> results = new List<RaycastResult>();
PointerEventData eventData = new PointerEventData(eventSystem);
eventData.position = Input.mousePosition;
raycaster.Raycast(eventData, results);
return results.Any(result => result.gameObject == gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment