Skip to content

Commit 78c114d

Browse files
committed
Setup popup panel through DI instead of a singleton
1 parent 764ccc6 commit 78c114d

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed

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:7b2c33d4681d53ec62b32b7ff2c81123b4457d8dcd8d0b6d643097e74ae36856
3-
size 35006
2+
oid sha256:13b1c488ad1662e224891150417b6946a08a8e84cbb0a3436bf15aa630faea99
3+
size 35452

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class ClientMainMenuState : GameStateBehaviour
3232
[SerializeField] CanvasGroup m_MainMenuButtonsCanvasGroup;
3333
[SerializeField] GameObject m_SignInSpinner;
3434

35+
PopupPanel m_PopupPanel;
36+
3537
void Awake()
3638
{
3739
m_MainMenuButtonsCanvasGroup.interactable = false;
@@ -40,7 +42,7 @@ void Awake()
4042
}
4143

4244
[Inject]
43-
void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby)
45+
void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFacade, LocalLobbyUser localUser, LocalLobby localLobby, PopupPanel popupPanel)
4446
{
4547
m_Scope = new DIScope(DIScope.RootScope);
4648

@@ -49,6 +51,8 @@ void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFaca
4951

5052
var unityAuthenticationInitOptions = new InitializationOptions();
5153

54+
m_PopupPanel = popupPanel;
55+
5256
#if UNITY_EDITOR
5357
//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
5458
//This allows us to test services integration locally by utilising Parrelsync.
@@ -100,7 +104,7 @@ void OnAuthSignIn()
100104

101105
void OnSignInFailed()
102106
{
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.");
107+
m_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.");
104108
}
105109
}
106110

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ namespace Unity.Multiplayer.Samples.BossRoom.Visual
77
public class ConnectionStatusMessageUIManager : MonoBehaviour
88
{
99
IDisposable m_Subscriptions;
10+
PopupPanel m_PopupPanel;
1011

1112
[Inject]
12-
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub)
13+
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSub, PopupPanel popupPanel)
1314
{
1415
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
16+
m_PopupPanel = popupPanel;
1517
}
1618

1719
void Awake()
@@ -32,15 +34,15 @@ void OnConnectStatus(ConnectStatus status)
3234
case ConnectStatus.UserRequestedDisconnect:
3335
break;
3436
case ConnectStatus.ServerFull:
35-
PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
37+
m_PopupPanel.ShowPopupPanel("Connection Failed", "The Host is full and cannot accept any additional connections.");
3638
break;
3739
case ConnectStatus.Success:
3840
break;
3941
case ConnectStatus.LoggedInAgain:
40-
PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
42+
m_PopupPanel.ShowPopupPanel("Connection Failed", "You have logged in elsewhere using the same account.");
4143
break;
4244
case ConnectStatus.GenericDisconnect:
43-
PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost");
45+
m_PopupPanel.ShowPopupPanel("Disconnected From Host", "The connection to the host was lost");
4446
break;
4547
default:
4648
Debug.LogWarning($"New ConnectStatus {status} has been added, but no connect message defined for it.");

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class LobbyUIMediator : MonoBehaviour
2424
NameGenerationData m_NameGenerationData;
2525
GameNetPortal m_GameNetPortal;
2626
ClientGameNetPortal m_ClientNetPortal;
27+
PopupPanel m_PopupPanel;
2728

2829
[Inject]
2930
void InjectDependenciesAndInitialize(
@@ -33,7 +34,8 @@ void InjectDependenciesAndInitialize(
3334
LocalLobby localLobby,
3435
NameGenerationData nameGenerationData,
3536
GameNetPortal gameNetPortal,
36-
ClientGameNetPortal clientGameNetPortal
37+
ClientGameNetPortal clientGameNetPortal,
38+
PopupPanel popupPanel
3739
)
3840
{
3941
m_NameGenerationData = nameGenerationData;
@@ -42,6 +44,7 @@ ClientGameNetPortal clientGameNetPortal
4244
m_LocalLobby = localLobby;
4345
m_GameNetPortal = gameNetPortal;
4446
m_ClientNetPortal = clientGameNetPortal;
47+
m_PopupPanel = popupPanel;
4548

4649
RegenerateName();
4750

@@ -151,7 +154,7 @@ void OnJoinedLobby(Lobby remoteLobby)
151154

152155
void OnRelayJoinFailed(string message)
153156
{
154-
PopupPanel.ShowPopupPanel("Relay join failed", message);
157+
m_PopupPanel.ShowPopupPanel("Relay join failed", message);
155158
Debug.Log($"Relay join failed: {message}");
156159
//leave the lobby if relay failed for some reason
157160
m_LobbyServiceFacade.EndTracking();

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
55
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
66
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
7+
using Unity.Multiplayer.Samples.BossRoom.Visual;
78
using Unity.Netcode;
89
using UnityEngine;
910
using UnityEngine.SceneManagement;
@@ -15,9 +16,17 @@ namespace Unity.Multiplayer.Samples.BossRoom.Shared
1516
/// </summary>
1617
public class ApplicationController : MonoBehaviour
1718
{
18-
[SerializeField] UpdateRunner m_UpdateRunner;
19-
[SerializeField] GameNetPortal m_GameNetPortal;
20-
[SerializeField] ClientGameNetPortal m_ClientNetPortal;
19+
[SerializeField]
20+
UpdateRunner m_UpdateRunner;
21+
22+
[SerializeField]
23+
GameNetPortal m_GameNetPortal;
24+
25+
[SerializeField]
26+
ClientGameNetPortal m_ClientNetPortal;
27+
28+
[SerializeField]
29+
PopupPanel m_PopupPanel;
2130

2231
LocalLobby m_LocalLobby;
2332
LobbyServiceFacade m_LobbyServiceFacade;
@@ -37,6 +46,7 @@ private void Awake()
3746
scope.BindInstanceAsSingle(m_UpdateRunner);
3847
scope.BindInstanceAsSingle(m_GameNetPortal);
3948
scope.BindInstanceAsSingle(m_ClientNetPortal);
49+
scope.BindInstanceAsSingle(m_PopupPanel);
4050

4151
//the following singletons represent the local representations of the lobby that we're in and the user that we are
4252
//they can persist longer than the lifetime of the UI in MainMenu where we set up the lobby that we create or join

Assets/BossRoom/Scripts/Client/UI/PopupPanel.cs renamed to Assets/BossRoom/Scripts/Shared/Game/UI/PopupPanel.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,11 @@ public class PopupPanel : MonoBehaviour
1616

1717
bool m_IsPopupShown;
1818

19-
static PopupPanel s_Instance;
20-
2119
void Awake()
2220
{
23-
if (s_Instance != null) throw new Exception("Invalid state, instance is not null");
24-
s_Instance = this;
2521
ResetState();
2622
}
2723

28-
void OnDestroy()
29-
{
30-
s_Instance = null;
31-
}
32-
3324
public void OnConfirmClick()
3425
{
3526
ResetState();
@@ -51,16 +42,9 @@ void ResetState()
5142
/// </summary>
5243
/// <param name="titleText">The title text at the top of the panel</param>
5344
/// <param name="mainText"> The text just under the title- the main body of text</param>
54-
public static void ShowPopupPanel(string titleText, string mainText)
45+
public void ShowPopupPanel(string titleText, string mainText)
5546
{
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-
}
47+
SetupPopupPanel(titleText, mainText);
6448
}
6549

6650
void SetupPopupPanel(string titleText, string mainText)

0 commit comments

Comments
 (0)