Skip to content

Commit df31161

Browse files
feat: popup panel for connection status messages (#494)
* Added basic popup panel for connection status and auth errors * Added a message channel for ConnectionStatus * Created manager to handle connection status messages * Displaying new messages to console if popup window is already in use
1 parent 87eaaa2 commit df31161

File tree

10 files changed

+681
-65
lines changed

10 files changed

+681
-65
lines changed

Assets/BossRoom/Prefabs/UI/SettingsPanelCanvas.prefab

Lines changed: 503 additions & 5 deletions
Large diffs are not rendered by default.

Assets/BossRoom/Scenes/Startup.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:7f94d9ef30f99dd13a417106572a3d3dda71b434c4f9a41b7b54a717971bb6a8
3-
size 33791
2+
oid sha256:7b2c33d4681d53ec62b32b7ff2c81123b4457d8dcd8d0b6d643097e74ae36856
3+
size 35006

Assets/BossRoom/Scripts/Client/Game/State/ClientMainMenuState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void OnAuthSignIn()
100100

101101
void OnSignInFailed()
102102
{
103-
Debug.LogError("For some reason we can't authenticate the user anonymously - that typically means that project is not properly set up with Unity services.");
103+
PopupPanel.ShowPopupPanel("Authentication Error", "For some reason we can't authenticate the user anonymously - that typically means that project is not properly set up with Unity services.");
104104
}
105105
}
106106

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
3+
using UnityEngine;
4+
5+
namespace Unity.Multiplayer.Samples.BossRoom.Visual
6+
{
7+
/// <summary>
8+
/// Subscribes to connection status messages to display them through the popup panel.
9+
/// </summary>
10+
public class ConnectionStatusMessageUIManager : MonoBehaviour
11+
{
12+
IDisposable m_Subscriptions;
13+
14+
[Inject]
15+
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub)
16+
{
17+
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
18+
}
19+
20+
void Awake()
21+
{
22+
DontDestroyOnLoad(gameObject);
23+
}
24+
25+
void OnDestroy()
26+
{
27+
m_Subscriptions?.Dispose();
28+
}
29+
30+
void OnConnectStatus(ConnectStatus status)
31+
{
32+
switch (status)
33+
{
34+
case ConnectStatus.Undefined:
35+
case ConnectStatus.UserRequestedDisconnect:
36+
break;
37+
case ConnectStatus.ServerFull:
38+
PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
39+
break;
40+
case ConnectStatus.Success:
41+
break;
42+
case ConnectStatus.LoggedInAgain:
43+
PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
44+
break;
45+
case ConnectStatus.GenericDisconnect:
46+
PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost");
47+
break;
48+
default:
49+
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
50+
break;
51+
}
52+
}
53+
}
54+
}

Assets/BossRoom/Scripts/Client/UI/ConnectionStatusMessageUIManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,13 @@ ClientGameNetPortal clientGameNetPortal
4646
RegenerateName();
4747

4848
m_ClientNetPortal.NetworkTimedOut += OnNetworkTimeout;
49-
m_ClientNetPortal.ConnectFinished += OnConnectFinished;
50-
51-
//any disconnect reason set? Show it to the user here.
52-
ConnectStatusToMessage(m_ClientNetPortal.DisconnectReason.Reason, false);
53-
m_ClientNetPortal.DisconnectReason.Clear();
5449
}
5550

5651
void OnDestroy()
5752
{
5853
if (m_ClientNetPortal != null)
5954
{
6055
m_ClientNetPortal.NetworkTimedOut -= OnNetworkTimeout;
61-
m_ClientNetPortal.ConnectFinished -= OnConnectFinished;
6256
}
6357
}
6458

@@ -157,6 +151,7 @@ void OnJoinedLobby(Lobby remoteLobby)
157151

158152
void OnRelayJoinFailed(string message)
159153
{
154+
PopupPanel.ShowPopupPanel("Relay join failed", message);
160155
Debug.Log($"Relay join failed: {message}");
161156
//leave the lobby if relay failed for some reason
162157
m_LobbyServiceFacade.EndTracking();
@@ -217,15 +212,6 @@ void UnblockUIAfterLoadingIsComplete()
217212
}
218213
}
219214

220-
/// <summary>
221-
/// Callback when the server sends us back a connection finished event.
222-
/// </summary>
223-
/// <param name="status"></param>
224-
void OnConnectFinished(ConnectStatus status)
225-
{
226-
ConnectStatusToMessage(status, true);
227-
}
228-
229215
/// <summary>
230216
/// Invoked when the client sent a connection request to the server and didn't hear back at all.
231217
/// This should create a UI letting the player know that something went wrong and to try again
@@ -234,40 +220,5 @@ void OnNetworkTimeout()
234220
{
235221
UnblockUIAfterLoadingIsComplete();
236222
}
237-
238-
/// <summary>
239-
/// Takes a ConnectStatus and shows an appropriate message to the user. This can be called on: (1) successful connect,
240-
/// (2) failed connect, (3) disconnect.
241-
/// </summary>
242-
/// <param name="connecting">pass true if this is being called in response to a connect finishing.</param>
243-
void ConnectStatusToMessage(ConnectStatus status, bool connecting)
244-
{
245-
switch (status)
246-
{
247-
case ConnectStatus.Undefined:
248-
case ConnectStatus.UserRequestedDisconnect:
249-
break;
250-
case ConnectStatus.ServerFull:
251-
Debug.Log("Connection Failed, The Host is full and cannot accept any additional connections");
252-
break;
253-
case ConnectStatus.Success:
254-
if (connecting)
255-
{
256-
Debug.Log("Success!, Joining Now");
257-
}
258-
break;
259-
case ConnectStatus.LoggedInAgain:
260-
Debug.Log("Connection Failed, You have logged in elsewhere using the same account");
261-
break;
262-
case ConnectStatus.GenericDisconnect:
263-
var title = connecting ? "Connection Failed" : "Disconnected From Host";
264-
var text = connecting ? "Something went wrong" : "The connection to the host was lost";
265-
Debug.Log($"{title}, {text}");
266-
break;
267-
default:
268-
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");
269-
break;
270-
}
271-
}
272223
}
273224
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using UnityEngine;
3+
using TMPro;
4+
5+
namespace Unity.Multiplayer.Samples.BossRoom.Visual
6+
{
7+
/// <summary>
8+
/// Simple popup panel to display information to players.
9+
/// </summary>
10+
public class PopupPanel : MonoBehaviour
11+
{
12+
[SerializeField]
13+
TextMeshProUGUI m_TitleText;
14+
[SerializeField]
15+
TextMeshProUGUI m_MainText;
16+
17+
bool m_IsPopupShown;
18+
19+
static PopupPanel s_Instance;
20+
21+
void Awake()
22+
{
23+
if (s_Instance != null) throw new Exception("Invalid state, instance is not null");
24+
s_Instance = this;
25+
ResetState();
26+
}
27+
28+
void OnDestroy()
29+
{
30+
s_Instance = null;
31+
}
32+
33+
public void OnConfirmClick()
34+
{
35+
ResetState();
36+
}
37+
38+
/// <summary>
39+
/// Helper method to help us reset all state for the popup.
40+
/// </summary>
41+
void ResetState()
42+
{
43+
m_TitleText.text = string.Empty;
44+
m_MainText.text = string.Empty;
45+
gameObject.SetActive(false);
46+
m_IsPopupShown = false;
47+
}
48+
49+
/// <summary>
50+
/// Sets the panel to match the given specifications to notify the player. If display image is set to true, it will display
51+
/// </summary>
52+
/// <param name="titleText">The title text at the top of the panel</param>
53+
/// <param name="mainText"> The text just under the title- the main body of text</param>
54+
public static void ShowPopupPanel(string titleText, string mainText)
55+
{
56+
if (s_Instance != null)
57+
{
58+
s_Instance.SetupPopupPanel(titleText, mainText);
59+
}
60+
else
61+
{
62+
Debug.LogError($"No PopupPanel instance found. Cannot display message: {titleText}: {mainText}");
63+
}
64+
}
65+
66+
void SetupPopupPanel(string titleText, string mainText)
67+
{
68+
if (m_IsPopupShown)
69+
{
70+
Debug.Log("Trying to show popup, but another popup is already being shown.");
71+
Debug.Log($"{titleText}. {mainText}");
72+
}
73+
else
74+
{
75+
ResetState();
76+
77+
m_TitleText.text = titleText;
78+
m_MainText.text = mainText;
79+
80+
gameObject.SetActive(true);
81+
m_IsPopupShown = true;
82+
}
83+
}
84+
}
85+
}

Assets/BossRoom/Scripts/Client/UI/PopupPanel.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ private void Awake()
4646
//this message channel is essential and persists for the lifetime of the lobby and relay services
4747
scope.BindMessageChannel<UnityServiceErrorMessage>();
4848

49+
//this message channel is essential and persists for the lifetime of the lobby and relay services
50+
scope.BindMessageChannel<ConnectStatus>();
51+
4952
//buffered message channels hold the latest received message in buffer and pass to any new subscribers
5053
scope.BindBufferedMessageChannel<LobbyListFetchedMessage>();
5154

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ public class ClientGameNetPortal : MonoBehaviour
2121
/// <summary>
2222
/// If a disconnect occurred this will be populated with any contextual information that was available to explain why.
2323
/// </summary>
24-
public DisconnectReason DisconnectReason { get; private set; } = new DisconnectReason();
24+
public DisconnectReason DisconnectReason { get; } = new DisconnectReason();
2525

2626
/// <summary>
2727
/// Time in seconds before the client considers a lack of server response a timeout
2828
/// </summary>
2929
private const int k_TimeoutDuration = 10;
3030

31-
public event Action<ConnectStatus> ConnectFinished;
32-
3331
/// <summary>
3432
/// This event fires when the client sent out a request to start the client, but failed to hear back after an allotted amount of
3533
/// time from the host.
3634
/// </summary>
3735
public event Action NetworkTimedOut;
3836

3937
private LobbyServiceFacade m_LobbyServiceFacade;
38+
IPublisher<ConnectStatus> m_ConnectStatusPub;
4039

4140
[Inject]
42-
private void InjectDependencies(LobbyServiceFacade lobbyServiceFacade)
41+
private void InjectDependencies(LobbyServiceFacade lobbyServiceFacade, IPublisher<ConnectStatus> connectStatusPub)
4342
{
4443
m_LobbyServiceFacade = lobbyServiceFacade;
44+
m_ConnectStatusPub = connectStatusPub;
4545
}
4646

4747
private void Awake()
@@ -107,8 +107,10 @@ public void OnConnectFinished(ConnectStatus status)
107107
//this indicates a game level failure, rather than a network failure. See note in ServerGameNetPortal.
108108
DisconnectReason.SetDisconnectReason(status);
109109
}
110-
111-
ConnectFinished?.Invoke(status);
110+
else
111+
{
112+
m_ConnectStatusPub.Publish(status);
113+
}
112114
}
113115

114116
private void OnDisconnectReasonReceived(ConnectStatus status)
@@ -133,14 +135,15 @@ private void OnDisconnectOrTimeout(ulong clientID)
133135
//disconnect that happened for some other reason than user UI interaction--should display a message.
134136
DisconnectReason.SetDisconnectReason(ConnectStatus.GenericDisconnect);
135137
}
136-
137138
SceneManager.LoadScene("MainMenu");
138139
}
139140
else if (DisconnectReason.Reason == ConnectStatus.GenericDisconnect || DisconnectReason.Reason == ConnectStatus.Undefined)
140141
{
141142
// only call this if generic disconnect. Else if there's a reason, there's already code handling that popup
142143
NetworkTimedOut?.Invoke();
143144
}
145+
m_ConnectStatusPub.Publish(DisconnectReason.Reason);
146+
DisconnectReason.Clear();
144147
}
145148
}
146149

0 commit comments

Comments
 (0)