Objects jump/warp for a little distance after regain the focus in Android
Hello, I met this problem for some time. The GameObjects is set to start a random movement by give their rigidbody2D a velocity in Start().
However, if I switch to the multi-app or say running-app page, and refocus to the game, they warp for a little distance. And according to the Time.time, the game time also past almost one second.
Here are two mp4s. I don't know if they are clear enough, I edited the first one to show the problem in 0.5 speed, and in the end a 0.25 replay for the key part, you can keep eyes on one of the moving GameObjects to see the warp, and pay a attention of the time text in another round.
The first text is Time.time*100+Time.deltatime*10000,
2nd counter for OnApplicationPause and OnApplicationFocus,
3rd I used this to find the order of execution of the two events in different situation,
4th deltatime between each event fired(P-pause, F-focus, D-Defocus).
If this description is too messy, you can read the scripts in the end of this question. According to the deltatime text, it seems no game time between the lost focus and regain focus. And for standalone build, the GameObjects work smoothly on my PC without this problem while I switch between different windows.
So, is this a problem only present in android? And since no problem for PC, it indicates no problem in scripts or settings of the game? And, no "solution" for it?
well, looks ugly, I admit.
The codes:
//Codes not directly related to this issue is ignored.
public class script : MonoBehaviour
{
public GameObject[] Roller;
public Text TimeText;
public Text PauseText; //To see how many times the events were fired
private int pause=0;
private int focus = 0;
private int defocus = 0;
private bool isPause = true;
public Text Order; //To show the sequence of events
public Text Delta; // To show game deltatime between event.
float timer;
void Start ()
{
PauseText.text = "Paused Times: " + pause + " " + "Focused Times: " + focus + " DeFocus Times: " + defocus;
Order.text="";Delta.text="";timer=0f;
}
void Update ()
{
TimeText.text = "Time passed*100: " + (int)(Time.time*100) + " " + "DeltaTime*10000:" + (int) (Time.deltaTime * 10000);
PauseText.text = "Paused Times: " + pause + " " + "Focused Times: " + focus + " DeFocus Times: " + defocus;
}
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
pause++;
}
Order.text=Order.text+"PauseCalled ";
Delta.text=Delta.text+" P"+(int)((Time.time-timer)*10000);
timer=Time.time;
}
private void OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
focus++;Order.text=Order.text+"FocusCalled ";Delta.text=Delta.text+" F"+(int)((Time.time-timer)*10000);Time.timeScale=1;
}
else
{
defocus++;Order.text=Order.text+"DeFocusCalled ";Delta.text=Delta.text+" D"+(int)((Time.time-timer)*10000);Time.timeScale=0;
}
timer=Time.time;
}
}