简体   繁体   English

在 Unity 中触摸结束后,有没有办法找到上次按下的项目是否是 UI 元素

[英]Is there a way to find if last pressed item was a UI element after touch has ended in Unity

I'm making a 2D infinite run platform game in which the player jumps after the Touch input ends.我正在制作一个 2D 无限运行平台游戏,其中玩家在触摸输入结束后跳跃。 My game has some UI button elements in it too.我的游戏中也有一些 UI 按钮元素。 But my character is jumping when i'm returning from main paused screen to game screen.但是当我从暂停的主屏幕返回到游戏屏幕时,我的角色正在跳跃。 Here is the code i tried.There is some issue with touch.phase==touchphase.end and not working together.but for some reason JumpAndResetPower() gets executed even though i clicked on resume button on UI.这是我试过的代码。 touch.phase==touchphase.end 存在一些问题并且不能一起工作。但是由于某种原因,即使我点击了 UI 上的恢复按钮, JumpAndResetPower() 也会被执行。

if(Input.touchCount>0)
    {

        Touch touch = Input.GetTouch(0);

        if(touch.phase==TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            accumilateJumpPower();
        }
        else if(touch.phase==TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            JumpAndResetPower();
        }
    }

The way I got around it is by using aa variable that's set every time I press the pause button.我绕过它的方法是使用每次按下暂停按钮时设置的 aa 变量。 And later when I press the play button, the jumping code will only get executed if variable is not set.稍后当我按下播放按钮时,只有在未设置变量的情况下才会执行跳转代码。 Else if variable is set it will unset the variable.否则,如果设置了变量,它将取消设置变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM