private Button GetButtonUnderCursor()
{
// Player camera ray
_mainCameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(_mainCameraRay, out RaycastHit hit, 1f, _screenLayer))
{
// UI-camera ray - convert the hit texture coordinates into camera coordinates
_uiCameraRay = _uiCamera.ViewportPointToRay(hit.textureCoord);
if (Physics.Raycast(_uiCameraRay, out RaycastHit Hit, 1f, _uiLayer))
{
// if the ray hits a button, return the button
Button button = Hit.collider.GetComponent< Button >();
if (button != null)
{
return button;
}
}
}
return null;
}