Unityでは、Physics.Raycast
を使ってマウスがオブジェクト上にあるかどうか調べることができます。
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
// 処理したい内容
}
ただし、上のスクリプトはUIでは使えません。
マウスがUIのオブジェクト上にあるかどうか調べたい場合はUnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject
を使うと良いそうです。
using UnityEngine.EventSystems;
if (EventSystem.current.IsPointerOverGameObject ()) {
// 処理したい内容
}
リンク
Unity – スクリプトリファレンス: EventSystems.EventSystem.IsPointerOverGameObject
https://docs.unity3d.com/jp/current/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html