AdditiveNetworkManager.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. namespace Mirror.Examples.Additive
  5. {
  6. [AddComponentMenu("")]
  7. public class AdditiveNetworkManager : NetworkManager
  8. {
  9. [Tooltip("Trigger Zone Prefab")]
  10. public GameObject Zone;
  11. [Scene]
  12. [Tooltip("Add all sub-scenes to this list")]
  13. public string[] subScenes;
  14. public override void OnStartServer()
  15. {
  16. base.OnStartServer();
  17. // load all subscenes on the server only
  18. StartCoroutine(LoadSubScenes());
  19. // Instantiate Zone Handler on server only
  20. Instantiate(Zone);
  21. }
  22. IEnumerator LoadSubScenes()
  23. {
  24. Debug.Log("Loading Scenes");
  25. foreach (string sceneName in subScenes)
  26. {
  27. yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
  28. // Debug.Log($"Loaded {sceneName}");
  29. }
  30. }
  31. public override void OnStopServer()
  32. {
  33. StartCoroutine(UnloadScenes());
  34. }
  35. public override void OnStopClient()
  36. {
  37. StartCoroutine(UnloadScenes());
  38. }
  39. IEnumerator UnloadScenes()
  40. {
  41. Debug.Log("Unloading Subscenes");
  42. foreach (string sceneName in subScenes)
  43. if (SceneManager.GetSceneByName(sceneName).IsValid() || SceneManager.GetSceneByPath(sceneName).IsValid())
  44. {
  45. yield return SceneManager.UnloadSceneAsync(sceneName);
  46. // Debug.Log($"Unloaded {sceneName}");
  47. }
  48. yield return Resources.UnloadUnusedAssets();
  49. }
  50. }
  51. }