Unity判断鼠标是否在UI层

//鼠标是否在UI层
public virtual bool IsPointerOnUI(string UILayerName=”UI“)
{
  bool isPointerOnUI = false;
  if (EventSystem.current.IsPointerOverGameObject())
  {  
    PointerEventData pointerData = new PointerEventData(EventSystem.current);
    pointerData.position = Input.mousePosition;

    List<RaycastResult> results = new List<RaycastResult>();
    EventSystem.current.RaycastAll(pointerData, results);

    for (int i = 0; i < results.Count; ++i)
    {
      if (results[i].gameObject.layer == LayerMask.NameToLayer(UILayerName))
      {
        isPointerOnUI = true;
        break;
      }
    }
  }

  return isPointerOnUI;  

}

posted on 2021-07-05 11:07  you_yond  阅读(300)  评论(0)    收藏  举报

导航