使用以下代码即可:
if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
if(Input.touchCount>0 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
return true;
}
else
return false;
}
else {
if(Input.GetMouseButton(0) && EventSystem.current.IsPointerOverGameObject()) {
return true;
}
else {
return false;
}
}
在手机上EventSystem.current.IsPointerOverGameObject()是只检测鼠标左键,加参数是为了在移动设备上检测touch的ID。一般移动设备上第一个触摸为0,但是鼠标左键被UGUI定义为-1。