Skip to content

Commit

Permalink
feat: server sends list of additive scenes upon connect (#451)
Browse files Browse the repository at this point in the history
* feat: load all additive scenes on client

* better comments
  • Loading branch information
uweeby committed Nov 2, 2020
1 parent bdb3083 commit 3d0b6c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions Assets/Mirror/Runtime/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct SceneMessage
public string scenePath;
// Normal = 0, LoadAdditive = 1, UnloadAdditive = 2
public SceneOperation sceneOperation;
public string[] additiveScenes;
}

public struct SceneReadyMessage { }
Expand Down
39 changes: 33 additions & 6 deletions Assets/Mirror/Runtime/NetworkSceneManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
Expand Down Expand Up @@ -53,6 +54,11 @@ public class NetworkSceneManager : MonoBehaviour, INetworkSceneManager

internal AsyncOperation asyncOperation;

//Used by the server to track all additive scenes. To notify clients upon connection
internal List<string> additiveSceneList = new List<string>();
//Used by the client to load the full additive scene list that the server has upon connection
internal List<string> pendingAdditiveSceneList = new List<string>();

public void Start()
{
DontDestroyOnLoad(gameObject);
Expand Down Expand Up @@ -110,6 +116,18 @@ internal void ClientSceneMessage(INetworkConnection conn, SceneMessage msg)
// Let client prepare for scene change
OnClientChangeScene(msg.scenePath, msg.sceneOperation);

//Additive are scenes loaded on server and this client is not a host client
if(msg.additiveScenes != null && msg.additiveScenes.Length > 0)
{
if(client && !client.IsLocalClient)
{
foreach (string scene in msg.additiveScenes)
{
pendingAdditiveSceneList.Add(scene);
}
}
}

StartCoroutine(ApplySceneOperation(msg.scenePath, msg.sceneOperation));
}

Expand Down Expand Up @@ -148,11 +166,18 @@ internal void OnClientChangeScene(string scenePath, SceneOperation sceneOperatio
/// <param name="sceneOperation">Scene operation that was just happen</param>
internal void OnClientSceneChanged(string scenePath, SceneOperation sceneOperation)
{
ClientSceneChanged.Invoke(scenePath, sceneOperation);

if (pendingAdditiveSceneList.Count > 0 && client && !client.IsLocalClient)
{
StartCoroutine(ApplySceneOperation(pendingAdditiveSceneList[0], SceneOperation.LoadAdditive));
pendingAdditiveSceneList.RemoveAt(0);
return;
}

//set ready after scene change has completed
if (!client.Connection.IsReady)
SetClientReady();

ClientSceneChanged.Invoke(scenePath, sceneOperation);
}

/// <summary>
Expand Down Expand Up @@ -183,7 +208,7 @@ void OnServerAuthenticated(INetworkConnection conn)
{
logger.Log("NetworkSceneManager.OnServerAuthenticated");

conn.Send(new SceneMessage { scenePath = NetworkScenePath });
conn.Send(new SceneMessage { scenePath = NetworkScenePath, additiveScenes = additiveSceneList.ToArray() });
conn.Send(new SceneReadyMessage());
}

Expand Down Expand Up @@ -265,6 +290,7 @@ IEnumerator ApplySceneOperation(string scenePath, SceneOperation sceneOperation
if (!SceneManager.GetSceneByPath(scenePath).IsValid())
{
yield return SceneManager.LoadSceneAsync(scenePath, LoadSceneMode.Additive);
additiveSceneList.Add(scenePath);
FinishLoadScene(scenePath, sceneOperation);
}
else
Expand All @@ -277,6 +303,7 @@ IEnumerator ApplySceneOperation(string scenePath, SceneOperation sceneOperation
if (SceneManager.GetSceneByPath(scenePath).IsValid())
{
yield return SceneManager.UnloadSceneAsync(scenePath, UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
additiveSceneList.Remove(scenePath);
FinishLoadScene(scenePath, sceneOperation);
}
else
Expand All @@ -298,7 +325,7 @@ internal void FinishLoadScene(string scenePath, SceneOperation sceneOperation)
// host mode?
if (client && client.IsLocalClient)
{
logger.Log("Finished loading scene in host mode.");
if (logger.LogEnabled()) logger.Log("Host: " + sceneOperation.ToString() + " operation for scene: " + scenePath);

// call OnServerSceneChanged
OnServerSceneChanged(scenePath, sceneOperation);
Expand All @@ -312,14 +339,14 @@ internal void FinishLoadScene(string scenePath, SceneOperation sceneOperation)
// server-only mode?
else if (server && server.Active)
{
logger.Log("Finished loading scene in server-only mode.");
if (logger.LogEnabled()) logger.Log("Server: " + sceneOperation.ToString() + " operation for scene: " + scenePath);

OnServerSceneChanged(scenePath, sceneOperation);
}
// client-only mode?
else if (client && client.Active && !client.IsLocalClient)
{
logger.Log("Finished loading scene in client-only mode.");
if (logger.LogEnabled()) logger.Log("Client: " + sceneOperation.ToString() + " operation for scene: " + scenePath);

OnClientSceneChanged(scenePath, sceneOperation);
}
Expand Down

0 comments on commit 3d0b6c5

Please sign in to comment.