NetworkManagerHUD.cs 4.6 KB

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