Skip to content

fix: postgame menu button #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Assets/BossRoom/Scripts/Client/UI/PostGameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using Unity.Multiplayer.Samples.BossRoom.Shared;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.Utilities;
using Unity.Netcode;
using UnityEngine.Assertions;

namespace Unity.Multiplayer.Samples.BossRoom.Visual
{
Expand All @@ -12,6 +15,8 @@ namespace Unity.Multiplayer.Samples.BossRoom.Visual
/// </summary>
public class PostGameUI : MonoBehaviour
{
ApplicationController m_ApplicationController;

[SerializeField]
private Light m_SceneLight;

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

[Inject]
void InjectDependencies(ApplicationController applicationController)
{
m_ApplicationController = applicationController;
}

void Awake()
{
// This is needed because the post game UI is part of the PostGame scene so we need to manually inject dependencies on awake.
DIScope.RootScope.InjectIn(this);
}

void Start()
{
// only hosts can restart the game, other players see a wait message
Expand Down Expand Up @@ -77,18 +94,15 @@ void SetPostGameUI(WinState winState)
public void OnPlayAgainClicked()
{
// this should only ever be called by the Host - so just go ahead and switch scenes
Assert.IsTrue(NetworkManager.Singleton.IsServer);
SceneLoaderWrapper.Instance.LoadScene("CharSelect");

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

public void OnMainMenuClicked()
{
// Player is leaving this group - leave current network connection first
var gameNetPortal = GameObject.FindGameObjectWithTag("GameNetPortal").GetComponent<GameNetPortal>();
gameNetPortal.RequestDisconnect();

SceneLoaderWrapper.Instance.LoadScene("MainMenu");
m_ApplicationController.LeaveSession();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Assets/BossRoom/Scripts/Shared/ApplicationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
using Unity.Multiplayer.Samples.Utilities;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
Expand Down Expand Up @@ -106,7 +107,8 @@ public void LeaveSession()
{
gameNetPortal.RequestDisconnect();
}
SceneManager.LoadScene("MainMenu");

SceneLoaderWrapper.Instance.LoadScene("MainMenu");
}

public void QuitGame()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void AddOnSceneEventCallback()
/// <param name="loadSceneMode">If LoadSceneMode.Single then all current Scenes will be unloaded before loading.</param>
public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
{
if (m_NetworkManager != null && m_NetworkManager.IsListening && m_NetworkManager.NetworkConfig.EnableSceneManagement)
if (m_NetworkManager != null && m_NetworkManager.IsListening && !m_NetworkManager.ShutdownInProgress && m_NetworkManager.NetworkConfig.EnableSceneManagement)
{
if (m_NetworkManager.IsServer)
{
Expand All @@ -90,7 +90,7 @@ public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneM

void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
if (m_NetworkManager == null || !m_NetworkManager.IsListening || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
if (m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
{
m_ClientLoadingScreen.StopLoadingScreen();
}
Expand Down