Unity New Input System on Android not receive touch event after execute time consuming function

Viewed 281

This is demo video https://www.youtube.com/watch?v=2YeNwtXaC_0, the project is the New Input System Demo:Touch Samples;enter image description here

I add a time consuming function:

  public void DelayTime()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        while (stopwatch.ElapsedMilliseconds< 1000)
        {
            var vector3 = new Vector3(1.0254452f, 1544.454f, 846.2342412f);
            var magnitude = vector3.magnitude + vector3.sqrMagnitude;
            magnitude += Vector3.Angle(Random.insideUnitSphere, Random.insideUnitSphere);
        }
        stopwatch.Stop();
    }

When click the button,this function will be called.

The problem is when click the button one or more times,means android will freeze .When android freeze,I touch the screen a little times,and then the screen will never receive touch event from new input system.

1 Answers

The problem is not about the "input system". It's about the code snippet in DelayTime function.

You don't allow program to continue for 1000 milliseconds after touch. You say "For 1000 milliseconds, do only this job on this thread! Don't refresh screen, don't run other scripts etc."

Try this code, and it will be fixed.

public IEnumerator DelayTime()
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    while (stopwatch.ElapsedMilliseconds< 1000)
    {
        var vector3 = new Vector3(1.0254452f, 1544.454f, 846.2342412f);
        var magnitude = vector3.magnitude + vector3.sqrMagnitude;
        magnitude += Vector3.Angle(Random.insideUnitSphere, Random.insideUnitSphere);

        yield return null;
    }
    stopwatch.Stop();
}
Related