天天看點

判斷滑鼠或者手指是否點選在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。