Delay server sending networkSceneName to client

All right, here’s an overview of my server/client connection procedure

  1. (Standard Unity) Server starts and loads the Game scene (networkSceneName == “Game”)
  2. (Standard Unity) Client connects to server and automatically is told to load the Game scene
  3. (Custom) Server sends a manifest to Client telling it which AssetBundles to load, one of these include the Game scene
  4. (Custom) Client loads said AssetBundles and tells the server it is ready

Now this naturally produces an error because the client tries to load the scene “Game” before my custom asset loading logic has run. What I would want to do is manually send the ClientChangeScene message during step 4, instead of having Unity automatically doing it as early as possible (I’m guessing during OnServerConnectInternal).

Does anyone have any experience with this? It all seems very internal to me.

The two solutions I can think of is (1) do a fully custom connection at first, negotiating all the asset loading (steps 3, 4) before initiating the full on Unity built-in client/server connection, or (2) override Awake() in my NetworkManager subclass, causing the ClientChangeScene message not being sent (and probably affecting other behaviour) and sending it manually by reverse engineering the UDP payload. None of which feels optimal…

Any help would be dandy.

xoxo


PS: This isn’t a problem if the server doesn’t change scene until after the client is ready, which will make everything work as expected, but I want players to be able to join a game in progress, and still run my custom code.

OK so after some further investigation I found the cleanest way is to ignore the message being sent and letting it fail.

The message typ is MsgType.Scene so I simply created my own message class and sent the message again at a later point, when I knew the client was done loading all the bundles.


SceneMessage.cs

using UnityEngine;
using UnityEngine.Networking;

public class SceneMessage : MessageBase
{
    public string scene;

    public SceneMessage(string inScene)
    {
        scene = inScene;
    }
}

Usage

// This is run on the server,
// in a message handler for a "load complete" message sent from the client.
var message = new SceneMessage(NetworkManager.networkSceneName);
netMessage.conn.Send(MsgType.Scene, message);

This might help someone else, or not… :slight_smile: