serverworld, clientworld

This commit is contained in:
vis2k 2021-05-16 17:27:27 +08:00
parent df003586f2
commit 6bb01517cd

View File

@ -6,9 +6,11 @@ public class NetMan : MonoBehaviour
// Server/Client worlds for easy access
public const string ClientWorldName = "ClientWorld";
public const string ServerWorldName = "ServerWorld";
public Scene ClientWorld;
public Scene ServerWorld;
Scene originalScene;
public static Scene ClientWorld;
public static Scene ServerWorld;
static Scene originalScene;
static string originalScenePath;
static bool initialized = false;
// helper function to LoadScene with a custom name
// -> LoadScene additive can duplicate but not change the name
@ -22,8 +24,14 @@ void LoadSceneCustomName(string scenePath, LoadSceneParameters parameters, strin
void Awake()
{
// we only do world initialization ONCE
// duplicating a scene would call Awake here again.
if (initialized) return;
initialized = true;
// remember the original scene
originalScene = SceneManager.GetActiveScene();
originalScenePath = originalScene.path;
// let's create a ServerWorld and ClientWorld like in DOTSNET
// so that even if we connect client 1 hour after starting server,
@ -36,14 +44,32 @@ void Awake()
// can't merge any scenes yet because not loaded in Awake() yet.
// setup OnSceneLoaded callback and continue there.
SceneManager.sceneLoaded += OnSceneLoaded;
// duplicate original scene. we'll move it into ClientWorld
// -> additive, otherwise we would just reload it
SceneManager.LoadScene(originalScene.path, LoadSceneMode.Additive);
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
Debug.Log($"OnSceneLoaded: {scene.name} {scene.path}");
// is this for the original scene?
if (scene == originalScene)
{
SceneManager.MergeScenes(originalScene, ClientWorld);
Debug.Log($"original scene loaded");
// merge original into ServerWorld
SceneManager.MergeScenes(scene, ServerWorld);
Debug.Log($"Original scene merged into {ServerWorldName}!");
}
// not the same scene, but same path. so it's the duplicate.
else if (scene.path == originalScenePath)
{
Debug.Log($"duplicated scene loaded");
// merge duplicate into serverworld
SceneManager.MergeScenes(scene, ClientWorld);
Debug.Log($"Original scene merged into {ClientWorldName}!");
}
}