- Home /
UNet NetworkManager Client Disconnect : After OnApplicationFocus
When I put an iOS app in the background, Unity sends OnApplicationFocus with the argument true.
Whenever I bring the app back to the foreground, the client UNet Client Disconnect from server
Answer by skuzio · Oct 25, 2017 at 07:56 PM
I recently ran across an issue similar to what I think is happening to you. When the app is put into the background either by pressing the home button or locking the screen, something goes wrong with UNet and all sorts of strange connection issues crop up.
The best way I found to avoid these issues is to disconnect as the app is being put into the background, and reconnect when the app is brought to the foreground. something like this:
void OnApplicationPause(bool paused)
{
if(!paused)
{
// App coming into foreground
NetworkManager.singleton.StartClient();
}
else
{
// App getting suspended
NetworkManager.singleton.client.Disconnect();
}
}
Yes, you are still technically disconnecting from the match, but it seems to lead to more predictable behavior and avoids some bugs that may potentially arise from going into a suspended state while still trying to maintain a connection.