NetworkRoomManagerExt.cs 3.2 KB

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