Skip to content

Commit 9e68691

Browse files
authored
fix: postgame menu button (#523)
* fix: fix postgame return to menu button to work and quit the active lobby * Remove private modifiers
1 parent 4668566 commit 9e68691

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
using UnityEngine.SceneManagement;
33
using UnityEngine.UI;
44
using TMPro;
5+
using Unity.Multiplayer.Samples.BossRoom.Shared;
6+
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
57
using Unity.Multiplayer.Samples.Utilities;
68
using Unity.Netcode;
9+
using UnityEngine.Assertions;
710

811
namespace Unity.Multiplayer.Samples.BossRoom.Visual
912
{
@@ -12,6 +15,8 @@ namespace Unity.Multiplayer.Samples.BossRoom.Visual
1215
/// </summary>
1316
public class PostGameUI : MonoBehaviour
1417
{
18+
ApplicationController m_ApplicationController;
19+
1520
[SerializeField]
1621
private Light m_SceneLight;
1722

@@ -36,6 +41,18 @@ public class PostGameUI : MonoBehaviour
3641
[SerializeField]
3742
private Color m_LoseLightColor;
3843

44+
[Inject]
45+
void InjectDependencies(ApplicationController applicationController)
46+
{
47+
m_ApplicationController = applicationController;
48+
}
49+
50+
void Awake()
51+
{
52+
// This is needed because the post game UI is part of the PostGame scene so we need to manually inject dependencies on awake.
53+
DIScope.RootScope.InjectIn(this);
54+
}
55+
3956
void Start()
4057
{
4158
// only hosts can restart the game, other players see a wait message
@@ -77,18 +94,15 @@ void SetPostGameUI(WinState winState)
7794
public void OnPlayAgainClicked()
7895
{
7996
// this should only ever be called by the Host - so just go ahead and switch scenes
97+
Assert.IsTrue(NetworkManager.Singleton.IsServer);
8098
SceneLoaderWrapper.Instance.LoadScene("CharSelect");
8199

82100
// FUTURE: could be improved to better support a dedicated server architecture
83101
}
84102

85103
public void OnMainMenuClicked()
86104
{
87-
// Player is leaving this group - leave current network connection first
88-
var gameNetPortal = GameObject.FindGameObjectWithTag("GameNetPortal").GetComponent<GameNetPortal>();
89-
gameNetPortal.RequestDisconnect();
90-
91-
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
105+
m_ApplicationController.LeaveSession();
92106
}
93107
}
94108
}

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 3 additions & 1 deletion
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.Utilities;
78
using Unity.Netcode;
89
using UnityEngine;
910
using UnityEngine.SceneManagement;
@@ -106,7 +107,8 @@ public void LeaveSession()
106107
{
107108
gameNetPortal.RequestDisconnect();
108109
}
109-
SceneManager.LoadScene("MainMenu");
110+
111+
SceneLoaderWrapper.Instance.LoadScene("MainMenu");
110112
}
111113

112114
public void QuitGame()

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void AddOnSceneEventCallback()
6969
/// <param name="loadSceneMode">If LoadSceneMode.Single then all current Scenes will be unloaded before loading.</param>
7070
public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
7171
{
72-
if (m_NetworkManager != null && m_NetworkManager.IsListening && m_NetworkManager.NetworkConfig.EnableSceneManagement)
72+
if (m_NetworkManager != null && m_NetworkManager.IsListening && !m_NetworkManager.ShutdownInProgress && m_NetworkManager.NetworkConfig.EnableSceneManagement)
7373
{
7474
if (m_NetworkManager.IsServer)
7575
{
@@ -90,7 +90,7 @@ public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneM
9090

9191
void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
9292
{
93-
if (m_NetworkManager == null || !m_NetworkManager.IsListening || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
93+
if (m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
9494
{
9595
m_ClientLoadingScreen.StopLoadingScreen();
9696
}

0 commit comments

Comments
 (0)