LoginSignup
1

【Unity】RectTransform内にカーソルが入ったことを検知する

Posted at

SortingLayerなどのせいでEventTriggerが使えなくなったとき、地道にマウスポインタがRectTransform内に入っているのか検出するには、次の関数をご使用あれ。

private bool CheckPointerIn()
{
    if (Input.mousePresent)
    {
        RectTransform rectTransform = GetComponent<RectTransform>();

        Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 localMousePosition = rectTransform.InverseTransformPoint(worldPosition);
        bool isMouseInRectTransform = rectTransform.rect.Contains(localMousePosition);

        if (isMouseInRectTransform)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        //マウスデバイスがない
        return false;
    }
}

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
What you can do with signing up
1