MatchNetworkManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace Mirror.Examples.MultipleMatch
  4. {
  5. [AddComponentMenu("")]
  6. public class MatchNetworkManager : NetworkManager
  7. {
  8. [Header("Match GUI")]
  9. public GameObject canvas;
  10. public CanvasController canvasController;
  11. public static new MatchNetworkManager singleton { get; private set; }
  12. /// <summary>
  13. /// Runs on both Server and Client
  14. /// Networking is NOT initialized when this fires
  15. /// </summary>
  16. public override void Awake()
  17. {
  18. base.Awake();
  19. singleton = this;
  20. canvasController.InitializeData();
  21. }
  22. #region Server System Callbacks
  23. /// <summary>
  24. /// Called on the server when a client is ready.
  25. /// <para>The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.</para>
  26. /// </summary>
  27. /// <param name="conn">Connection from client.</param>
  28. public override void OnServerReady(NetworkConnectionToClient conn)
  29. {
  30. base.OnServerReady(conn);
  31. canvasController.OnServerReady(conn);
  32. }
  33. /// <summary>
  34. /// Called on the server when a client disconnects.
  35. /// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
  36. /// </summary>
  37. /// <param name="conn">Connection from client.</param>
  38. public override void OnServerDisconnect(NetworkConnectionToClient conn)
  39. {
  40. StartCoroutine(DoServerDisconnect(conn));
  41. }
  42. IEnumerator DoServerDisconnect(NetworkConnectionToClient conn)
  43. {
  44. yield return canvasController.OnServerDisconnect(conn);
  45. base.OnServerDisconnect(conn);
  46. }
  47. #endregion
  48. #region Client System Callbacks
  49. /// <summary>
  50. /// Called on the client when connected to a server.
  51. /// <para>The default implementation of this function sets the client as ready and adds a player. Override the function to dictate what happens when the client connects.</para>
  52. /// </summary>
  53. public override void OnClientConnect()
  54. {
  55. base.OnClientConnect();
  56. canvasController.OnClientConnect();
  57. }
  58. /// <summary>
  59. /// Called on clients when disconnected from a server.
  60. /// <para>This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.</para>
  61. /// </summary>
  62. public override void OnClientDisconnect()
  63. {
  64. canvasController.OnClientDisconnect();
  65. base.OnClientDisconnect();
  66. }
  67. #endregion
  68. #region Start & Stop Callbacks
  69. /// <summary>
  70. /// This is invoked when a server is started - including when a host is started.
  71. /// <para>StartServer has multiple signatures, but they all cause this hook to be called.</para>
  72. /// </summary>
  73. public override void OnStartServer()
  74. {
  75. if (mode == NetworkManagerMode.ServerOnly)
  76. canvas.SetActive(true);
  77. canvasController.OnStartServer();
  78. }
  79. /// <summary>
  80. /// This is invoked when the client is started.
  81. /// </summary>
  82. public override void OnStartClient()
  83. {
  84. canvas.SetActive(true);
  85. canvasController.OnStartClient();
  86. }
  87. /// <summary>
  88. /// This is called when a server is stopped - including when a host is stopped.
  89. /// </summary>
  90. public override void OnStopServer()
  91. {
  92. canvasController.OnStopServer();
  93. canvas.SetActive(false);
  94. }
  95. /// <summary>
  96. /// This is called when a client is stopped.
  97. /// </summary>
  98. public override void OnStopClient()
  99. {
  100. canvasController.OnStopClient();
  101. }
  102. #endregion
  103. }
  104. }