C# Unity3D:在TouchEnded创建的计时器不工作

C# Unity3D:在TouchEnded创建的计时器不工作,c#,unity3d,timer,C#,Unity3d,Timer,使用Unity3d 2018.2.1和C# 问题:当用户单击屏幕时,启动计时器未启动,触摸未注册,因此计时器从未启动 在FixedUpdate()中,当用户触摸屏幕启动计时器时,我正在查看。 如果用户没有再次触摸屏幕,并且已经15秒了,那么请采取措施。如果用户在15秒前触摸屏幕,则重新启动计时器 public float uiStartTime = 0f; public float uiCurrentTime = 0f; public float uiMaxTime = 15f; privat

使用Unity3d 2018.2.1和C#

问题:当用户单击屏幕时,启动计时器未启动,触摸未注册,因此计时器从未启动

在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
    }
}

当使用浮动时,您的等式基本上永远不会为真
uiStartime-uiCurrentTime
也会给您一个负数。 相反,你应该:

   if (uiCurrentTime - uiStartTime >= uiMaxTime)
问题:当用户单击屏幕时StartTimer未启动,请触摸not 正在注册,所以计时器永远不会启动。我正在使用鼠标进行测试,但是 将为移动设备部署

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

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


如果您想让代码更兼容鼠标和触摸屏,那么就制作一个简单的函数,将触摸屏和鼠标API封装起来。然后,您可以结合使用
UNITY_STANDALONE
UNITY_EDITOR
预处理器指令来检测您是否在移动平台上运行

下面是一个简单的移动/桌面触摸包装器,它不需要Unity Remote 5:

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

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");
    }
}
public float uiStartTime=0;
公共浮点数uiMaxTime=15f;
私有布尔时间规划=假;
私有void更新()
{
//如果计时器正在运行,则递增计时器
如果(定时)
uiStartTime+=Time.deltaTime;
接触相接触相;
如果(触摸屏(外触摸阶段))
{
//检查keypres和启动计时器(如果未运行)
if(touchPhase==touchPhase.Ended&!timerRunning)
{
timerRunning=true;
}
//如果用户在15秒前触摸屏幕,则重新启动计时器。
else if(touchPhase==touchPhase.Ended&&timerRunning&&uiStartTime=uiMaxTime)
{
//做点什么
Debug.Log(“计时器在15秒内未被触摸”);
}
}

Put
Debud.Log
in
if(touch.phase==TouchPhase.Ended)
并查看是否会调用它dit not getcalled@Programmer此外,我使用鼠标进行测试,但会部署到移动设备上
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");
    }
}