Skip to content

Commit 9a6d601

Browse files
fix: workaround for fixing the loading screen never going away on clients (#549)
* workaround for fixing the loading screen never going away on clients if the main menu scene switches too fast. * removing useless log * Update Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs
1 parent 93ff764 commit 9a6d601

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,14 @@ public void OnConnectFinished(ConnectStatus status)
145145
}
146146
}
147147

148-
private void OnDisconnectReasonReceived(ConnectStatus status)
149-
{
150-
DisconnectReason.SetDisconnectReason(status);
151-
}
152-
153-
private void OnDisconnectOrTimeout(ulong clientID)
148+
void OnDisconnectOrTimeout(ulong clientID)
154149
{
155150
// This is also called on the Host when a different client disconnects. To make sure we only handle our own disconnection, verify that we are either
156151
// not a host (in which case we know this is about us) or that the clientID is the same as ours if we are the host.
157152
if (!NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsHost && NetworkManager.Singleton.LocalClientId == clientID)
158153
{
154+
SceneLoaderWrapper.Instance.IsClosingClients = false; // disconnect done
155+
159156
var lobbyCode = "";
160157
if (m_LobbyServiceFacade.CurrentUnityLobby != null)
161158
{
@@ -330,7 +327,8 @@ public static void ReceiveServerToClientConnectResult_CustomMessage(ulong client
330327
public static void ReceiveServerToClientSetDisconnectReason_CustomMessage(ulong clientID, FastBufferReader reader)
331328
{
332329
reader.ReadValueSafe(out ConnectStatus status);
333-
Instance.OnDisconnectReasonReceived(status);
330+
Instance.DisconnectReason.SetDisconnectReason(status);
331+
SceneLoaderWrapper.Instance.IsClosingClients = true;
334332
}
335333
}
336334
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,19 @@ public void OnUserDisconnectRequest()
134134
{
135135
SendServerToAllClientsSetDisconnectReason(ConnectStatus.HostDisconnected);
136136
// wait before shutting down to make sure those messages get sent before the clients disconnect.
137-
StartCoroutine(WaitToShutdown());
137+
StartCoroutine(WaitToShutdown()); // todo in theory, according to "Shutdown"'s doc, we shouldn't need this wait anymore.
138138
}
139139
}
140140

141141
IEnumerator WaitToShutdown()
142142
{
143-
yield return new WaitForSeconds(0.5f);
143+
// todo netmanager should really be shutting down instead of us having to manually flag that we're shutting down
144+
SceneLoaderWrapper.Instance.IsClosingClients = true;
145+
yield return null; // todo still needed? wait for UTP's update for it to send it's batched messages
146+
yield return null;
144147
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
145148
m_Portal.NetManager.Shutdown();
149+
SceneLoaderWrapper.Instance.IsClosingClients = false;
146150
}
147151

148152
void Clear()
@@ -267,6 +271,7 @@ public void SendServerToClientSetDisconnectReason(ulong clientID, ConnectStatus
267271
writer.WriteValueSafe(status);
268272
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage(nameof(ClientGameNetPortal.ReceiveServerToClientSetDisconnectReason_CustomMessage), clientID, writer);
269273
}
274+
270275
/// <summary>
271276
/// Sends a DisconnectReason to all connected clients. This should only be done on the server, prior to disconnecting the clients.
272277
/// </summary>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,16 @@ public void LoadScene(string sceneName, LoadSceneMode loadSceneMode = LoadSceneM
8888
}
8989
}
9090

91+
public bool IsClosingClients { get; set; }
92+
9193
void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
9294
{
93-
if (m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement)
95+
// we're letting networked scene loading handle our loading screen, since there's still some loading happening after unity scene loading is done.
96+
// in case we're offline, then we let unity's scene loader tell us when to turn off the loading screen.
97+
var isOffline = m_NetworkManager == null || !m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress || !m_NetworkManager.NetworkConfig.EnableSceneManagement;
98+
// 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
99+
// for this in MTT-2821
100+
if (isOffline || IsClosingClients)
94101
{
95102
m_ClientLoadingScreen.StopLoadingScreen();
96103
}

0 commit comments

Comments
 (0)