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. public override void OnStopServer()
  23. {
  24. StartCoroutine(UnloadScenes());
  25. }
  26. public override void OnStopClient()
  27. {
  28. StartCoroutine(UnloadScenes());
  29. }
  30. IEnumerator LoadSubScenes()
  31. {
  32. Debug.Log("Loading Scenes");
  33. foreach (string sceneName in subScenes)
  34. {
  35. yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
  36. // Debug.Log($"Loaded {sceneName}");
  37. }
  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. }