LoginSignup
43
33

More than 5 years have passed since last update.

uGUIに当たっているかを調べる

Last updated at Posted at 2015-04-20

Physics.Raycastで調べたいオブジェクトとuGUIが混在している場所で、
UIに当たっていたらRaycastを除外したい時などに利用できます。

public static bool IsUGUIHit(Vector3 _scrPos){ // Input.mousePosition
    PointerEventData pointer = new PointerEventData (EventSystem.current);
    pointer.position = _scrPos;
    List<RaycastResult> result = new List<RaycastResult> ();
    EventSystem.current.RaycastAll (pointer, result);
    return (result.Count > 0);
}

UI Imageなど、UIの当たりを検出したくない場合は
該当のUIに下記のスクリプトをアタッチすることで実現できます。

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIIgnoreHit : Button, ICanvasRaycastFilter {
    public bool IsRaycastLocationValid (Vector2 sp, Camera eventCamera){
        return false;
    }
}
43
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
33