NetworkManagerHUD

NetworkManagerHUD提供用于控制游戏的网络状态的默认用户界面。它还显示有关编辑器中NetworkManager当前状态的信息。

这是作为开发和测试的辅助工具提供的,并不打算包含在已发布的游戏中。

属性

属性功能
showGUI标志显示默认UI。
offsetXUI的水平像素偏移量。
offsetYUI的垂直像素偏移量

细节

NetworkManagerHUD允许您在编辑器中查看许多细节,例如连接,已知网络对象列表和活动玩家对象。

源码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using UnityEngine.Networking.Match;
  5. namespace UnityEngine.Networking
  6. {
  7. [EditorBrowsable(EditorBrowsableState.Never), AddComponentMenu("Network/NetworkManagerHUD"), RequireComponent(typeof(NetworkManager))]
  8. public class NetworkManagerHUD : MonoBehaviour
  9. {
  10. public NetworkManager manager;
  11. [SerializeField]
  12. public bool showGUI = true;
  13. [SerializeField]
  14. public int offsetX;
  15. [SerializeField]
  16. public int offsetY;
  17. private bool m_ShowServer;
  18. private void Awake()
  19. {
  20. this.manager = base.GetComponent<NetworkManager>();
  21. }
  22. private void Update()
  23. {
  24. if (!this.showGUI)
  25. {
  26. return;
  27. }
  28. if (!this.manager.IsClientConnected() && !NetworkServer.active && this.manager.matchMaker == null)
  29. {
  30. if (Application.platform != RuntimePlatform.WebGLPlayer)
  31. {
  32. if (Input.GetKeyDown(KeyCode.S))
  33. {
  34. this.manager.StartServer();
  35. }
  36. if (Input.GetKeyDown(KeyCode.H))
  37. {
  38. this.manager.StartHost();
  39. }
  40. }
  41. if (Input.GetKeyDown(KeyCode.C))
  42. {
  43. this.manager.StartClient();
  44. }
  45. }
  46. if (NetworkServer.active && this.manager.IsClientConnected() && Input.GetKeyDown(KeyCode.X))
  47. {
  48. this.manager.StopHost();
  49. }
  50. }
  51. private void OnGUI()
  52. {
  53. if (!this.showGUI)
  54. {
  55. return;
  56. }
  57. int num = 10 + this.offsetX;
  58. int num2 = 40 + this.offsetY;
  59. bool flag = this.manager.client == null || this.manager.client.connection == null || this.manager.client.connection.connectionId == -1;
  60. if (!this.manager.IsClientConnected() && !NetworkServer.active && this.manager.matchMaker == null)
  61. {
  62. if (flag)
  63. {
  64. if (Application.platform != RuntimePlatform.WebGLPlayer)
  65. {
  66. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "LAN Host(H)"))
  67. {
  68. this.manager.StartHost();
  69. }
  70. num2 += 24;
  71. }
  72. if (GUI.Button(new Rect((float)num, (float)num2, 105f, 20f), "LAN Client(C)"))
  73. {
  74. this.manager.StartClient();
  75. }
  76. this.manager.networkAddress = GUI.TextField(new Rect((float)(num + 100), (float)num2, 95f, 20f), this.manager.networkAddress);
  77. num2 += 24;
  78. if (Application.platform == RuntimePlatform.WebGLPlayer)
  79. {
  80. GUI.Box(new Rect((float)num, (float)num2, 200f, 25f), "( WebGL cannot be server )");
  81. num2 += 24;
  82. }
  83. else
  84. {
  85. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "LAN Server Only(S)"))
  86. {
  87. this.manager.StartServer();
  88. }
  89. num2 += 24;
  90. }
  91. }
  92. else
  93. {
  94. GUI.Label(new Rect((float)num, (float)num2, 200f, 20f), string.Concat(new object[]
  95. {
  96. "Connecting to ",
  97. this.manager.networkAddress,
  98. ":",
  99. this.manager.networkPort,
  100. ".."
  101. }));
  102. num2 += 24;
  103. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Cancel Connection Attempt"))
  104. {
  105. this.manager.StopClient();
  106. }
  107. }
  108. }
  109. else
  110. {
  111. if (NetworkServer.active)
  112. {
  113. string text = "Server: port=" + this.manager.networkPort;
  114. if (this.manager.useWebSockets)
  115. {
  116. text += " (Using WebSockets)";
  117. }
  118. GUI.Label(new Rect((float)num, (float)num2, 300f, 20f), text);
  119. num2 += 24;
  120. }
  121. if (this.manager.IsClientConnected())
  122. {
  123. GUI.Label(new Rect((float)num, (float)num2, 300f, 20f), string.Concat(new object[]
  124. {
  125. "Client: address=",
  126. this.manager.networkAddress,
  127. " port=",
  128. this.manager.networkPort
  129. }));
  130. num2 += 24;
  131. }
  132. }
  133. if (this.manager.IsClientConnected() && !ClientScene.ready)
  134. {
  135. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Client Ready"))
  136. {
  137. ClientScene.Ready(this.manager.client.connection);
  138. if (ClientScene.localPlayers.Count == 0)
  139. {
  140. ClientScene.AddPlayer(0);
  141. }
  142. }
  143. num2 += 24;
  144. }
  145. if (NetworkServer.active || this.manager.IsClientConnected())
  146. {
  147. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Stop (X)"))
  148. {
  149. this.manager.StopHost();
  150. }
  151. num2 += 24;
  152. }
  153. if (!NetworkServer.active && !this.manager.IsClientConnected() && flag)
  154. {
  155. num2 += 10;
  156. if (Application.platform == RuntimePlatform.WebGLPlayer)
  157. {
  158. GUI.Box(new Rect((float)(num - 5), (float)num2, 220f, 25f), "(WebGL cannot use Match Maker)");
  159. return;
  160. }
  161. if (this.manager.matchMaker == null)
  162. {
  163. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Enable Match Maker (M)"))
  164. {
  165. this.manager.StartMatchMaker();
  166. }
  167. num2 += 24;
  168. }
  169. else
  170. {
  171. if (this.manager.matchInfo == null)
  172. {
  173. if (this.manager.matches == null)
  174. {
  175. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Create Internet Match"))
  176. {
  177. this.manager.matchMaker.CreateMatch(this.manager.matchName, this.manager.matchSize, true, string.Empty, string.Empty, string.Empty, 0, 0, new NetworkMatch.DataResponseDelegate<MatchInfo>(this.manager.OnMatchCreate));
  178. }
  179. num2 += 24;
  180. GUI.Label(new Rect((float)num, (float)num2, 100f, 20f), "Room Name:");
  181. this.manager.matchName = GUI.TextField(new Rect((float)(num + 100), (float)num2, 100f, 20f), this.manager.matchName);
  182. num2 += 24;
  183. num2 += 10;
  184. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Find Internet Match"))
  185. {
  186. this.manager.matchMaker.ListMatches(0, 20, string.Empty, false, 0, 0, new NetworkMatch.DataResponseDelegate<List<MatchInfoSnapshot>>(this.manager.OnMatchList));
  187. }
  188. num2 += 24;
  189. }
  190. else
  191. {
  192. for (int i = 0; i < this.manager.matches.Count; i++)
  193. {
  194. MatchInfoSnapshot matchInfoSnapshot = this.manager.matches[i];
  195. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Join Match:" + matchInfoSnapshot.name))
  196. {
  197. this.manager.matchName = matchInfoSnapshot.name;
  198. this.manager.matchMaker.JoinMatch(matchInfoSnapshot.networkId, string.Empty, string.Empty, string.Empty, 0, 0, new NetworkMatch.DataResponseDelegate<MatchInfo>(this.manager.OnMatchJoined));
  199. }
  200. num2 += 24;
  201. }
  202. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Back to Match Menu"))
  203. {
  204. this.manager.matches = null;
  205. }
  206. num2 += 24;
  207. }
  208. }
  209. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Change MM server"))
  210. {
  211. this.m_ShowServer = !this.m_ShowServer;
  212. }
  213. if (this.m_ShowServer)
  214. {
  215. num2 += 24;
  216. if (GUI.Button(new Rect((float)num, (float)num2, 100f, 20f), "Local"))
  217. {
  218. this.manager.SetMatchHost("localhost", 1337, false);
  219. this.m_ShowServer = false;
  220. }
  221. num2 += 24;
  222. if (GUI.Button(new Rect((float)num, (float)num2, 100f, 20f), "Internet"))
  223. {
  224. this.manager.SetMatchHost("mm.unet.unity3d.com", 443, true);
  225. this.m_ShowServer = false;
  226. }
  227. num2 += 24;
  228. if (GUI.Button(new Rect((float)num, (float)num2, 100f, 20f), "Staging"))
  229. {
  230. this.manager.SetMatchHost("staging-mm.unet.unity3d.com", 443, true);
  231. this.m_ShowServer = false;
  232. }
  233. }
  234. num2 += 24;
  235. GUI.Label(new Rect((float)num, (float)num2, 300f, 20f), "MM Uri: " + this.manager.matchMaker.baseUri);
  236. num2 += 24;
  237. if (GUI.Button(new Rect((float)num, (float)num2, 200f, 20f), "Disable Match Maker"))
  238. {
  239. this.manager.StopMatchMaker();
  240. }
  241. num2 += 24;
  242. }
  243. }
  244. }
  245. }
  246. }

?