Create CustomNetworkManager which prevents client from marking itself as ready until the Loaded finishes

This commit is contained in:
in0finite 2019-04-27 02:25:16 +02:00
parent 00ac3b5153
commit b66c73457f
3 changed files with 85 additions and 2 deletions

View file

@ -0,0 +1,74 @@
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Behaviours;
namespace SanAndreasUnity.Net
{
public class CustomNetworkManager : NetworkManager
{
public override void OnClientConnect(NetworkConnection conn)
{
if (NetStatus.IsServer)
{
// just do default action
base.OnClientConnect(conn);
return;
}
// default method: if no scene was loaded, do Ready/AddPlayer
// we won't do this until loading process finishes
if (Loader.HasLoaded)
base.OnClientConnect(conn);
}
public override void OnClientSceneChanged(NetworkConnection conn)
{
if (NetStatus.IsServer)
{
// just do default action
base.OnClientSceneChanged(conn);
return;
}
// default method: do Ready/AddPlayer
// we won't do this until loading process finishes
if (Loader.HasLoaded)
base.OnClientSceneChanged(conn);
}
void OnLoaderFinished()
{
if (NetStatus.IsServer) // don't do anything on server
return;
if (!NetworkClient.isConnected)
{
// client is not connected ? hmm... then loading process could not have started
Debug.LogErrorFormat("Loader finished, but client is not connected");
return;
}
// make client ready
if (ClientScene.ready)
Debug.LogErrorFormat("Client was made ready before loader finished");
else
ClientScene.Ready(NetworkClient.connection);
// add player if specified
if (autoCreatePlayer && ClientScene.localPlayer == null)
{
ClientScene.AddPlayer();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 155a544cb11c9481ea41b4e39d8c4b3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -8,8 +8,6 @@
- send input to server - send input to server
- make console available in main menu
- client must not be marked as ready, until Loader finishes - client must not be marked as ready, until Loader finishes