Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 6921b8e

Browse files
author
gkassabli
authored
[UTY-1613] Gracefully handle disconnect in Playground (#686)
* Handle disconnect gracefully on mobile clients * Addressing PR feedback * Adding changelog entry
1 parent 5d37da5 commit 6921b8e

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added support for the Alpha Locator flow.
99
- Added support for connecting mobile devices to cloud deployments via the anonymous authentication flow.
1010
- Added option to build workers out via IL2CPP in the cmd.
11+
- Added an example of handling disconnect for mobile workers.
1112

1213
### Changed
1314

workers/unity/Assets/Playground/Scripts/Camera/InitCameraSystem.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ protected override void OnUpdate()
3636
PostUpdateCommands.AddComponent(entity, CameraComponentDefaults.Transform);
3737

3838
Cursor.lockState = CursorLockMode.Locked;
39+
40+
// Disable system after first run.
41+
Enabled = false;
3942
}
4043
}
44+
45+
protected override void OnDestroyManager()
46+
{
47+
Cursor.lockState = CursorLockMode.None;
48+
}
4149
}
4250
}

workers/unity/Assets/Playground/Scripts/UI/ConnectionScreenController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public void OnConnectionFailed(string connectionError)
5959
$"Connection failed. Please check the IP address entered.\nSpatialOS error message:\n{connectionError}";
6060
}
6161

62+
public void OnDisconnected(string disconnectReason)
63+
{
64+
UnityObjectDestroyer.Destroy(worker);
65+
ipAddressInput.text = PlayerPrefs.GetString(HostIpPlayerPrefsKey);
66+
connectionPanel.SetActive(true);
67+
}
68+
6269
private IMobileConnectionController PrepareConnect()
6370
{
6471
errorMessage.text = string.Empty;

workers/unity/Assets/Playground/Scripts/UI/InitUISystem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,10 @@ protected override void OnUpdate()
4545
// Disable system after first run.
4646
Enabled = false;
4747
}
48+
49+
protected override void OnDestroyManager()
50+
{
51+
UnityObjectDestroyer.Destroy(UIComponent.Main.gameObject);
52+
}
4853
}
4954
}

workers/unity/Assets/Playground/Scripts/Worker/AndroidClientWorkerConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async void TryConnectAsync(ConnectionService connectionService)
2828
protected override void HandleWorkerConnectionEstablished()
2929
{
3030
ConnectionScreenController.OnConnectionSucceeded();
31+
Worker.OnDisconnect += ConnectionScreenController.OnDisconnected;
3132
WorkerUtils.AddClientSystems(Worker.World);
3233

3334
if (level == null)

workers/unity/Assets/Playground/Scripts/Worker/iOSClientWorkerConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public async void TryConnectAsync(ConnectionService connectionService)
2727
protected override void HandleWorkerConnectionEstablished()
2828
{
2929
ConnectionScreenController.OnConnectionSucceeded();
30+
Worker.OnDisconnect += ConnectionScreenController.OnDisconnected;
3031
WorkerUtils.AddClientSystems(Worker.World);
3132

3233
if (level == null)

0 commit comments

Comments
 (0)