NetworkRoomManagerExt.cs 3.1 KB

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