繁体   English   中英

Unity3D:在TouchEnded创建的计时器不起作用

[英]Unity3D: Timer created at TouchEnded not working

使用Unity3d 2018.2.1和C#

问题:用户单击屏幕时StartTimer无法启动,触摸未注册,因此计时器永不启动。

在FixedUpdate()中,我查看用户何时触摸屏幕以启动计时器。 如果用户没有再触摸屏幕并且已经15秒钟,请执行一些操作。 如果用户在15秒之前触摸屏幕,请重新启动计时器。

public float uiStartTime = 0f;
public float uiCurrentTime = 0f;
public float uiMaxTime = 15f;

private void FixedUpdate()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Ended)
        {
            uiStartTime = Time.time;
        }
    }

    uiCurrentTime = Time.time;

    if (uiStartTime - uiCurrentTime == uiMaxTime)
    {
        //  Reset Timer
        uiStartTime = 0f;
        uiCurrentTime = 0f;

        // Do Something
    }
}

问题:用户单击屏幕时StartTimer无法启动,触摸未注册,因此计时器永不启动.Im使用鼠标进行测试,但将部署到移动设备上

这是这里的主要问题。 当然,您的代码中仍然存在一些逻辑错误,但是由于您使用鼠标来测试Input.touchCountInput.GetTouch API, Input.GetTouch计时器不应启动。 这两个API仅在移动设备上起作用,因为它们使用触摸而不是鼠标。

如果要在编辑器中使用它们,请使用Unity Remote 5 将其下载到您的移动设备上,然后通过编辑 -> 项目设置 -> 编辑器在编辑器中启用它,然后将设备连接到计算机。 您应该能够在编辑器中使用touch API,而无需构建它。 有关Unity Remote 5的更多信息,请参见这篇文章。


如果您希望代码与鼠标和触摸更加兼容,那么可以编写一个简单的函数,将触摸和鼠标API封装起来。 然后,您可以结合使用UNITY_STANDALONEUNITY_EDITOR预处理程序指令来检测何时不在移动平台上运行。

这是一个简单的移动/桌面触摸包装器,无需Unity Remote 5即可使用

bool ScreenTouched(out TouchPhase touchPhase)
{
    #if UNITY_STANDALONE || UNITY_EDITOR
    //DESKTOP COMPUTERS
    if (Input.GetMouseButtonDown(0))
    {
        touchPhase = TouchPhase.Began;
        return true;
    }

    if (Input.GetMouseButtonUp(0))
    {
        touchPhase = TouchPhase.Ended;
        return true;
    }
    touchPhase = TouchPhase.Canceled;
    return false;
    #else

    //MOBILE DEVICES
    if (Input.touchCount > 0)
    {
        touchPhase = Input.GetTouch(0).phase;
        return true;
    }
    touchPhase = TouchPhase.Canceled;
    return false;
    #endif
}

下面是如何使用它来获取您想要的问题的方法:

public float uiStartTime = 0;
public float uiMaxTime = 15f;
private bool timerRunning = false;

private void Update()
{
    //Increment timer if it's running
    if (timerRunning)
        uiStartTime += Time.deltaTime;

    TouchPhase touchPhase;

    if (ScreenTouched(out touchPhase))
    {
        //Check for keypres and start timer if it's not running
        if (touchPhase == TouchPhase.Ended && !timerRunning)
        {
            timerRunning = true;
        }

        //If the user touches the screen before the 15 seconds, then restart the timer.
        else if (touchPhase == TouchPhase.Ended && timerRunning && uiStartTime < uiMaxTime)
        {
            uiStartTime = 0f;
            Debug.Log("Timer Reset");
        }
    }

    //If the user hasn't touched the screen again and it has been 15 seconds then do something.
    if (uiStartTime >= uiMaxTime)
    {
        // Do Something
        Debug.Log("Timer not touched for 15 seconds");
    }
}

当使用浮点数时,您的相等性从根本上就不会成立。 uiStartime - uiCurrentTime也给您一个负数。 而是:

   if (uiCurrentTime - uiStartTime >= uiMaxTime)

暂无
暂无

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

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