NetworkRoomManagerExt.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. /*
  3. Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
  4. API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
  5. */
  6. namespace Mirror.Examples.NetworkRoom
  7. {
  8. [AddComponentMenu("")]
  9. public class NetworkRoomManagerExt : NetworkRoomManager
  10. {
  11. [Header("Spawner Setup")]
  12. [Tooltip("Reward Prefab for the Spawner")]
  13. public GameObject rewardPrefab;
  14. public static new NetworkRoomManagerExt singleton { get; private set; }
  15. /// <summary>
  16. /// Runs on both Server and Client
  17. /// Networking is NOT initialized when this fires
  18. /// </summary>
  19. public override void Awake()
  20. {
  21. base.Awake();
  22. singleton = this;
  23. }
  24. /// <summary>
  25. /// This is called on the server when a networked scene finishes loading.
  26. /// </summary>
  27. /// <param name="sceneName">Name of the new scene.</param>
  28. public override void OnRoomServerSceneChanged(string sceneName)
  29. {
  30. // spawn the initial batch of Rewards
  31. if (sceneName == GameplayScene)
  32. Spawner.InitialSpawn();
  33. }
  34. /// <summary>
  35. /// Called just after GamePlayer object is instantiated and just before it replaces RoomPlayer object.
  36. /// This is the ideal point to pass any data like player name, credentials, tokens, colors, etc.
  37. /// into the GamePlayer object as it is about to enter the Online scene.
  38. /// </summary>
  39. /// <param name="roomPlayer"></param>
  40. /// <param name="gamePlayer"></param>
  41. /// <returns>true unless some code in here decides it needs to abort the replacement</returns>
  42. public override bool OnRoomServerSceneLoadedForPlayer(NetworkConnectionToClient conn, GameObject roomPlayer, GameObject gamePlayer)
  43. {
  44. PlayerScore playerScore = gamePlayer.GetComponent<PlayerScore>();
  45. playerScore.index = roomPlayer.GetComponent<NetworkRoomPlayer>().index;
  46. return true;
  47. }
  48. public override void OnRoomStopClient()
  49. {
  50. base.OnRoomStopClient();
  51. }
  52. public override void OnRoomStopServer()
  53. {
  54. base.OnRoomStopServer();
  55. }
  56. /*
  57. This code below is to demonstrate how to do a Start button that only appears for the Host player
  58. showStartButton is a local bool that's needed because OnRoomServerPlayersReady is only fired when
  59. all players are ready, but if a player cancels their ready state there's no callback to set it back to false
  60. Therefore, allPlayersReady is used in combination with showStartButton to show/hide the Start button correctly.
  61. Setting showStartButton false when the button is pressed hides it in the game scene since NetworkRoomManager
  62. is set as DontDestroyOnLoad = true.
  63. */
  64. bool showStartButton;
  65. public override void OnRoomServerPlayersReady()
  66. {
  67. // calling the base method calls ServerChangeScene as soon as all players are in Ready state.
  68. if (Utils.IsHeadless())
  69. {
  70. base.OnRoomServerPlayersReady();
  71. }
  72. else
  73. {
  74. showStartButton = true;
  75. }
  76. }
  77. public override void OnGUI()
  78. {
  79. base.OnGUI();
  80. if (allPlayersReady && showStartButton && GUI.Button(new Rect(150, 300, 120, 20), "START GAME"))
  81. {
  82. // set to false to hide it in the game scene
  83. showStartButton = false;
  84. ServerChangeScene(GameplayScene);
  85. }
  86. }
  87. }
  88. }