Hello everyone,
I’m currently prototyping a mobile game with local multiplayer and I’m using UNET for networking: since the game does not to require Internet access, using UNET with one player acting as host seemed a natural fit.
Playing around with the prototype, one issue I want to address is that the whole networking communication pauses when the game is minimized on the host device (testing on Android); which is understandable, since the OS is pausing the whole Unity process.
The simplest way to address this seems to use the OnApplicationPause method to call a “network pause!” ClientRpc on all clients when the host minimizes the game; however, this is not currently working in my build as the ClientRpc is invoked on clients only after the host resumes the game. If I understood the problem correctly, Unity prepares data for the ClientRpc call on the host but the process is paused before Unity can actually send the data to all clients.
I also tried setting the NetworkManager.singleton.maxDelay field to 0, but it didn’t help.
You can find below the code I added to a NetworkBehaviour:
float t = 0;
private void OnApplicationPause(bool pause)
{
if (pause && isServer)
{
t = NetworkManager.singleton.maxDelay;
NetworkManager.singleton.maxDelay = 0; //this seems useless :-(
RpcPause();
}
}
[ClientRpc]
private void RpcPause()
{
Time.timeScale = 0;
Debug.LogError("Game paused!");
}
private void OnApplicationFocus(bool focus)
{
if (focus && isServer)
{
NetworkManager.singleton.maxDelay = t;
t = 0;
RpcResume();
}
}
[ClientRpc]
private void RpcResume()
{
Time.timeScale = 1;
Debug.LogError("Game resumed!");
}
I’m currently using HLAPI and a ClientRpc, but any other solution which result in a synchronous data send inside the OnApplicationPause method is fine (network message? call to Transport API?).
Thank you all for your time and help!
Roberto