判断鼠标或者手指是否点击在UI上(用于应对不能点击UI的情况)

  使用以下代码即可:

 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。

你可能感兴趣的:(Unity--UGUI,Unity,UGUI,点击检测)