マウスが重なっているUIオブジェクトを取得する【Unity】
やること
EventSystemを参照してマウスが指示している位置に重なっているUIオブジェクトを取得します!
取得するオブジェクトは、EventSystemのInspecterの下部にあるWindowのpointerEnterでも確認できます。
手順
まずUnityEngine.EventSystemsをusingします。
using UnityEngine.EventSystems;
EventSystemを参照します。(アタッチするなりスクリプトで参照するなり)
private EventSystem eventSystem;
eventSystem=FindObjectOfType<EventSystem>();
currentSelectedGameObjectでUIオブジェクトを取得できます。
private gameObject mouse=eventSystem.currentSelectedGameObject;
使い道
シーン遷移時にフェード画像を一番上に重ねてクリックできないようにする
とか
ButtonじゃないUIをボタンみたいに動作させる
とか
いろんなシチュエーションで使えそうです!