Mirror/Assets/Scenery/NetMan.cs

89 lines
3.0 KiB
C#
Raw Normal View History

2021-05-16 07:10:31 +00:00
using UnityEngine;
2021-05-16 07:55:39 +00:00
using UnityEngine.SceneManagement;
2021-05-16 07:10:31 +00:00
public class NetMan : MonoBehaviour
{
2021-05-16 07:55:39 +00:00
// Server/Client worlds for easy access
public const string ClientWorldName = "ClientWorld";
public const string ServerWorldName = "ServerWorld";
2021-05-16 09:27:27 +00:00
public static Scene ClientWorld;
public static Scene ServerWorld;
static Scene originalScene;
static string originalScenePath;
static bool initialized = false;
2021-05-16 07:55:39 +00:00
// helper function to LoadScene with a custom name
// -> LoadScene additive can duplicate but not change the name
// -> we need to Create & Merge for that
void LoadSceneCustomName(string scenePath, LoadSceneParameters parameters, string customName)
{
SceneManager.CreateScene(customName);
SceneManager.LoadScene(gameObject.scene.path, LoadSceneMode.Additive);
//SceneManager.MergeScenes();
}
void Awake()
{
2021-05-16 09:27:27 +00:00
// we only do world initialization ONCE
// duplicating a scene would call Awake here again.
if (initialized) return;
initialized = true;
2021-05-16 07:55:39 +00:00
// remember the original scene
originalScene = SceneManager.GetActiveScene();
2021-05-16 09:27:27 +00:00
originalScenePath = originalScene.path;
2021-05-16 07:55:39 +00:00
// let's create a ServerWorld and ClientWorld like in DOTSNET
// so that even if we connect client 1 hour after starting server,
// the client still starts with a fresh client scene like everyone else.
SceneManager.CreateScene(ClientWorldName);
SceneManager.CreateScene(ServerWorldName);
ClientWorld = SceneManager.GetSceneByName(ClientWorldName);
ServerWorld = SceneManager.GetSceneByName(ServerWorldName);
// can't merge any scenes yet because not loaded in Awake() yet.
// setup OnSceneLoaded callback and continue there.
SceneManager.sceneLoaded += OnSceneLoaded;
2021-05-16 09:27:27 +00:00
// duplicate original scene. we'll move it into ClientWorld
// -> additive, otherwise we would just reload it
SceneManager.LoadScene(originalScene.path, LoadSceneMode.Additive);
2021-05-16 07:55:39 +00:00
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
2021-05-16 09:27:27 +00:00
Debug.Log($"OnSceneLoaded: {scene.name} {scene.path}");
2021-05-16 07:55:39 +00:00
// is this for the original scene?
if (scene == originalScene)
{
2021-05-16 09:27:27 +00:00
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);
2021-05-16 07:55:39 +00:00
Debug.Log($"Original scene merged into {ClientWorldName}!");
}
}
2021-05-16 07:10:31 +00:00
void OnGUI()
{
if (GUI.Button(new Rect(5, 5, 100, 25), "Start Server"))
{
2021-05-16 07:55:39 +00:00
//
2021-05-16 07:10:31 +00:00
}
if (GUI.Button(new Rect(5, 30, 100, 25), "Start Client"))
{
}
}
}