Skip to content

fix: nullref on host when disconnecting #558

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class SceneLoaderWrapper : MonoBehaviour

public static SceneLoaderWrapper Instance { get; private set; }

public bool IsClosingClients { get; set; }

public void Awake()
{
if (Instance != null && Instance != this)
Expand Down Expand Up @@ -69,40 +71,42 @@ 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.ShutdownInProgress && m_NetworkManager.NetworkConfig.EnableSceneManagement)
if (IsOffline() || IsClosingClients)
{
if (m_NetworkManager.IsServer)
// If offline, load using SceneManager
var loadOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
if (loadSceneMode == LoadSceneMode.Single)
{
// If is active server and NetworkManager uses scene management, load scene using NetworkManager's SceneManager
m_NetworkManager.SceneManager.LoadScene(sceneName, loadSceneMode);
m_ClientLoadingScreen.StartLoadingScreen(sceneName, loadOperation);
}
}
else
{
// If offline, load using SceneManager
var loadOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
if (loadSceneMode == LoadSceneMode.Single)
if (m_NetworkManager.IsServer)
{
m_ClientLoadingScreen.StartLoadingScreen(sceneName, loadOperation);
// If is active server and NetworkManager uses scene management, load scene using NetworkManager's SceneManager
m_NetworkManager.SceneManager.LoadScene(sceneName, loadSceneMode);
}
}
}

public bool IsClosingClients { get; set; }

void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
// we're letting networked scene loading handle our loading screen, since there's still some loading happening after unity scene loading is done.
// in case we're offline, then we let unity's scene loader tell us when to turn off the loading screen.
var isOffline = m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement;
// TODO this can be called while we're still in the WaitForSeconds(0.5) while shutting down host side. which means we'll still be in theory connected. Waiting on fix
// for this in MTT-2821
if (isOffline || IsClosingClients)
if (IsOffline() || IsClosingClients)
{
m_ClientLoadingScreen.StopLoadingScreen();
}
}

bool IsOffline()
{
return m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement;
}

void OnSceneEvent(SceneEvent sceneEvent)
{
// Only executes on client
Expand Down