天天看点

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

  使用以下代码即可:

if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) {
            if(Input.touchCount> && EventSystem.current.IsPointerOverGameObject(Input.GetTouch().fingerId)) {
                return true;
            }
            else
                return false;
        }
        else {
            if(Input.GetMouseButton() && EventSystem.current.IsPointerOverGameObject()) {
                return true;
            }
            else {
                return false;
            }
        }
           

  在手机上EventSystem.current.IsPointerOverGameObject()是只检测鼠标左键,加参数是为了在移动设备上检测touch的ID。一般移动设备上第一个触摸为0,但是鼠标左键被UGUI定义为-1。