Hi all,
I am encountering an issue with the Time.unscaledDeltaTime not being properly set when the application is paused or out of focus. I am running the following script on Android device using builds from 2017.2.0f3 and 2018.2.20f1. This is the scenario I am trying;
-
Launch application
-
Once loaded, I lock the device
-
I start a timer (on another device) to keep track of how long the game has been actually suspended
-
I press the lock button on the device again and the application is resumed
-
Simultaneously I stop the timer
-
The first Update call that gets executed should report that the Time.unscaledDeltaTime since last frame
-
The expected result is that the Time.unscaledDeltaTime is equal to the time that the application has been paused (roughly the same to that reported by the timer)
-
What I actually get is a value less than expected, say if the game has been suspended for 40s the Time.unscaledDeltaTime is set to 16s.
-
This behaviour was noticed in 2018.2.20f1 but not reproducible on 2017.2.0f3 (works correctly here)
Can someone verify or explain why this is happening?
using System;
using System.Text;
using UnityEngine;
public class TimeDemo : MonoBehaviour
{
private bool logNextDeltaTime;
private float lastUnscaledTime;
private StringBuilder logBuilder;
private void Awake()
{
logBuilder = new StringBuilder();
}
private void Update()
{
if (logNextDeltaTime)
{
var diff = Time.unscaledTime - lastUnscaledTime;
logBuilder.AppendLine(string.Format("Application is updating frame {0} with time {1} (+{2}s). Time difference should be +{3}s", Time.frameCount, Time.unscaledTime, Time.unscaledDeltaTime, diff));
logNextDeltaTime = false;
}
}
private void OnApplicationFocus(bool focus)
{
logBuilder.AppendLine(string.Format("Application is focused - {0} in frame {1} with time {2}", focus, Time.frameCount, Time.unscaledTime));
if (!focus)
{
logBuilder.AppendLine(string.Format("Last unscaled time {0}", Time.unscaledTime));
lastUnscaledTime = Time.unscaledTime;
logNextDeltaTime = true;
}
}
private void OnApplicationPause(bool pause)
{
logBuilder.AppendLine(string.Format("Application is paused - {0} in frame {1} with time {2}", pause, Time.frameCount, Time.unscaledTime));
if (pause)
{
logBuilder.AppendLine(string.Format("Last unscaled time {0}", Time.unscaledTime));
lastUnscaledTime = Time.unscaledTime;
logNextDeltaTime = true;
}
}
private void OnGUI()
{
GUI.skin.GetStyle("label").fontSize = 35;
GUI.skin.GetStyle("button").fontSize = 35;
if(GUILayout.Button("Clear Log", GUILayout.Width(300), GUILayout.Height(100)))
{
logBuilder = new StringBuilder();
}
GUILayout.Label(logBuilder.ToString());
}
}