NetworkManagerHUD.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // vis2k: GUILayout instead of spacey += ...; removed Update hotkeys to avoid
  2. // confusion if someone accidentally presses one.
  3. using System;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. /// <summary>Shows NetworkManager controls in a GUI at runtime.</summary>
  8. [DisallowMultipleComponent]
  9. [AddComponentMenu("Network/NetworkManagerHUD")]
  10. [RequireComponent(typeof(NetworkManager))]
  11. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-manager-hud")]
  12. public class NetworkManagerHUD : MonoBehaviour
  13. {
  14. NetworkManager manager;
  15. // Deprecated 2021-02-24
  16. [Obsolete("showGUI will be removed unless someone has a valid use case. Simply use or don't use the HUD component.")]
  17. public bool showGUI = true;
  18. public int offsetX;
  19. public int offsetY;
  20. void Awake()
  21. {
  22. manager = GetComponent<NetworkManager>();
  23. }
  24. void OnGUI()
  25. {
  26. #pragma warning disable 618
  27. if (!showGUI) return;
  28. #pragma warning restore 618
  29. GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999));
  30. if (!NetworkClient.isConnected && !NetworkServer.active)
  31. {
  32. StartButtons();
  33. }
  34. else
  35. {
  36. StatusLabels();
  37. }
  38. // client ready
  39. if (NetworkClient.isConnected && !NetworkClient.ready)
  40. {
  41. if (GUILayout.Button("Client Ready"))
  42. {
  43. NetworkClient.Ready();
  44. if (NetworkClient.localPlayer == null)
  45. {
  46. NetworkClient.AddPlayer();
  47. }
  48. }
  49. }
  50. StopButtons();
  51. GUILayout.EndArea();
  52. }
  53. void StartButtons()
  54. {
  55. if (!NetworkClient.active)
  56. {
  57. // Server + Client
  58. if (Application.platform != RuntimePlatform.WebGLPlayer)
  59. {
  60. if (GUILayout.Button("Host (Server + Client)"))
  61. {
  62. manager.StartHost();
  63. }
  64. }
  65. // Client + IP
  66. GUILayout.BeginHorizontal();
  67. if (GUILayout.Button("Client"))
  68. {
  69. manager.StartClient();
  70. }
  71. manager.networkAddress = GUILayout.TextField(manager.networkAddress);
  72. GUILayout.EndHorizontal();
  73. // Server Only
  74. if (Application.platform == RuntimePlatform.WebGLPlayer)
  75. {
  76. // cant be a server in webgl build
  77. GUILayout.Box("( WebGL cannot be server )");
  78. }
  79. else
  80. {
  81. if (GUILayout.Button("Server Only")) manager.StartServer();
  82. }
  83. }
  84. else
  85. {
  86. // Connecting
  87. GUILayout.Label("Connecting to " + manager.networkAddress + "..");
  88. if (GUILayout.Button("Cancel Connection Attempt"))
  89. {
  90. manager.StopClient();
  91. }
  92. }
  93. }
  94. void StatusLabels()
  95. {
  96. // host mode
  97. // display separately because this always confused people:
  98. // Server: ...
  99. // Client: ...
  100. if (NetworkServer.active && NetworkClient.active)
  101. {
  102. GUILayout.Label($"<b>Host</b>: running via {Transport.activeTransport}");
  103. }
  104. // server only
  105. else if (NetworkServer.active)
  106. {
  107. GUILayout.Label($"<b>Server</b>: running via {Transport.activeTransport}");
  108. }
  109. // client only
  110. else if (NetworkClient.isConnected)
  111. {
  112. GUILayout.Label($"<b>Client</b>: connected to {manager.networkAddress} via {Transport.activeTransport}");
  113. }
  114. }
  115. void StopButtons()
  116. {
  117. // stop host if host mode
  118. if (NetworkServer.active && NetworkClient.isConnected)
  119. {
  120. if (GUILayout.Button("Stop Host"))
  121. {
  122. manager.StopHost();
  123. }
  124. }
  125. // stop client if client-only
  126. else if (NetworkClient.isConnected)
  127. {
  128. if (GUILayout.Button("Stop Client"))
  129. {
  130. manager.StopClient();
  131. }
  132. }
  133. // stop server if server-only
  134. else if (NetworkServer.active)
  135. {
  136. if (GUILayout.Button("Stop Server"))
  137. {
  138. manager.StopServer();
  139. }
  140. }
  141. }
  142. }
  143. }