在场景中有时候一些3D物体在UI元素下面,在点击UI的时候同时点击到3D物体,在UGUI中给了一个方法EventSystem.current.IsPointerOverGameObject(),该方法可以判断点击是否是在UI元素上
private void Update() {
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) {
Debug.Log("no click button");
}
}
private void ClickButton() {
Debug.Log("ClickButton");
}
代码在点击按钮时输出ClickButton,在点击其他地方时输出no click button