com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Closing the game on Android doesn't properly disconnect the client

Open SubatomicPlanets opened this issue 1 year ago • 4 comments

Description

On Android, when quitting the app, the server and other players don't get notified. Eventually it will time out due to heartbeats, but that is not ideal. When quitting the game on other platforms, such as Windows, it correctly notifies the server, and the player prefab gets destroyed.

Environment

  • OS: Android
  • Unity Version: 6000.0.25f1
  • Netcode Version: 2.1.1

Additional Context

Specifically tested on:

  • Samsung galaxy J5 2017
  • Meta quest 2

SubatomicPlanets avatar Nov 09 '24 00:11 SubatomicPlanets

As far as I know, this is a limitation of mobile operation systems, android will instantly freeze the app when put in the background and will terminate the process nearly instantly as well when the user closes the app. I'm interested if there is a workaround for this tho.

Moe-Baker avatar Nov 12 '24 09:11 Moe-Baker

We're also seeing this, which causes players to persist in the lobby indefinitely and blocks them from rejoining for some time.

Meta Quest 3

StephenHodgson avatar Dec 03 '24 13:12 StephenHodgson

Would love to have a solution/workaround for this too

aondw avatar Dec 09 '24 15:12 aondw

Hi, On mobile (at least I just tested on android) you get some time to execute a bit of code before the application loses focus or is closed. However you need more than Application.wantsToQuit or Application.quitting, you need to hook to MonoBehaviour's OnApplicationFocus for when the app loses focus or OnApplicationPause for when the app is put in background or closed. If the app lost focus or is paused you can call Multiplayer ISession's LeaveAsync to close the session. If instead you want to leave some grace timeout when the user puts the app in background before killing the session you can use the Lobby's Disconnect Removal Timeout that can be configured in your cloud dashboard/lobby settings.

mnachury-unity avatar Nov 03 '25 20:11 mnachury-unity