NetworkRoomManagerExt.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /// <summary>
  15. /// This is called on the server when a networked scene finishes loading.
  16. /// </summary>
  17. /// <param name="sceneName">Name of the new scene.</param>
  18. public override void OnRoomServerSceneChanged(string sceneName)
  19. {
  20. // spawn the initial batch of Rewards
  21. if (sceneName == GameplayScene)
  22. Spawner.InitialSpawn();
  23. }
  24. /// <summary>
  25. /// Called just after GamePlayer object is instantiated and just before it replaces RoomPlayer object.
  26. /// This is the ideal point to pass any data like player name, credentials, tokens, colors, etc.
  27. /// into the GamePlayer object as it is about to enter the Online scene.
  28. /// </summary>
  29. /// <param name="roomPlayer"></param>
  30. /// <param name="gamePlayer"></param>
  31. /// <returns>true unless some code in here decides it needs to abort the replacement</returns>
  32. public override bool OnRoomServerSceneLoadedForPlayer(NetworkConnectionToClient conn, GameObject roomPlayer, GameObject gamePlayer)
  33. {
  34. PlayerScore playerScore = gamePlayer.GetComponent<PlayerScore>();
  35. playerScore.index = roomPlayer.GetComponent<NetworkRoomPlayer>().index;
  36. return true;
  37. }
  38. public override void OnRoomStopClient()
  39. {
  40. base.OnRoomStopClient();
  41. }
  42. public override void OnRoomStopServer()
  43. {
  44. base.OnRoomStopServer();
  45. }
  46. /*
  47. This code below is to demonstrate how to do a Start button that only appears for the Host player
  48. showStartButton is a local bool that's needed because OnRoomServerPlayersReady is only fired when
  49. all players are ready, but if a player cancels their ready state there's no callback to set it back to false
  50. Therefore, allPlayersReady is used in combination with showStartButton to show/hide the Start button correctly.
  51. Setting showStartButton false when the button is pressed hides it in the game scene since NetworkRoomManager
  52. is set as DontDestroyOnLoad = true.
  53. */
  54. bool showStartButton;
  55. public override void OnRoomServerPlayersReady()
  56. {
  57. // calling the base method calls ServerChangeScene as soon as all players are in Ready state.
  58. #if UNITY_SERVER
  59. base.OnRoomServerPlayersReady();
  60. #else
  61. showStartButton = true;
  62. #endif
  63. }
  64. public override void OnGUI()
  65. {
  66. base.OnGUI();
  67. if (allPlayersReady && showStartButton && GUI.Button(new Rect(150, 300, 120, 20), "START GAME"))
  68. {
  69. // set to false to hide it in the game scene
  70. showStartButton = false;
  71. ServerChangeScene(GameplayScene);
  72. }
  73. }
  74. }
  75. }