forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkSceneManager.cs
More file actions
97 lines (90 loc) · 3.9 KB
/
Copy pathNetworkSceneManager.cs
File metadata and controls
97 lines (90 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MLAPI.NetworkingManagerComponents
{
public static class NetworkSceneManager
{
internal static HashSet<string> registeredSceneNames;
internal static Dictionary<string, uint> sceneNameToIndex;
internal static Dictionary<uint, string> sceneIndexToString;
private static Scene lastScene;
private static Scene nextScene;
private static bool isSwitching = false;
internal static uint CurrentSceneIndex = 0;
internal static void SetCurrentSceneIndex ()
{
if(!sceneNameToIndex.ContainsKey(SceneManager.GetActiveScene().name))
{
Debug.LogWarning("MLAPI: Scene switching is enabled but the current scene (" + SceneManager.GetActiveScene().name + ") is not regisered as a network scene.");
return;
}
CurrentSceneIndex = sceneNameToIndex[SceneManager.GetActiveScene().name];
}
public static void SwitchScene(string sceneName)
{
if(!NetworkingManager.singleton.NetworkConfig.EnableSceneSwitching)
{
Debug.LogWarning("MLAPI: Scene switching is not enabled");
return;
}
else if (isSwitching)
{
Debug.LogWarning("MLAPI: Scene switch already in progress");
return;
}
else if(!registeredSceneNames.Contains(sceneName))
{
Debug.LogWarning("MLAPI: The scene " + sceneName + " is not registered as a switchable scene.");
return;
}
CurrentSceneIndex = sceneNameToIndex[sceneName];
isSwitching = true;
lastScene = SceneManager.GetActiveScene();
AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
sceneLoad.completed += OnSceneLoaded;
using(MemoryStream stream = new MemoryStream(4))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(sceneNameToIndex[sceneName]);
}
NetworkingManager.singleton.Send("MLAPI_SWITCH_SCENE", "MLAPI_INTERNAL", stream.GetBuffer());
}
}
internal static void OnSceneSwitch(uint sceneIndex)
{
if (!NetworkingManager.singleton.NetworkConfig.EnableSceneSwitching)
{
Debug.LogWarning("MLAPI: Scene switching is not enabled but was requested by the server");
return;
}
else if (!sceneIndexToString.ContainsKey(sceneIndex) || registeredSceneNames.Contains(sceneIndexToString[sceneIndex]))
{
Debug.LogWarning("MLAPI: Server requested a scene switch to a non registered scene");
return;
}
lastScene = SceneManager.GetActiveScene();
AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneIndexToString[sceneIndex], LoadSceneMode.Additive);
sceneLoad.completed += OnSceneLoaded;
}
private static void OnSceneLoaded(AsyncOperation operation)
{
List<NetworkedObject> objectsToKeep = SpawnManager.spawnedObjects.Values.ToList();
//The last loaded scene
nextScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
for (int i = 0; i < objectsToKeep.Count; i++)
{
SceneManager.MoveGameObjectToScene(objectsToKeep[i].gameObject, nextScene);
}
AsyncOperation sceneLoad = SceneManager.UnloadSceneAsync(lastScene);
sceneLoad.completed += OnSceneUnload;
}
private static void OnSceneUnload(AsyncOperation operation)
{
isSwitching = false;
}
}
}