NetworkManagerHUD.cs
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.ComponentModel;
namespace UnityEngine.Networking
{
/// <summary>
/// An extension for the NetworkManager that displays a default HUD for controlling the network state of the game.
/// <para>This component also shows useful internal state for the networking system in the inspector window of the editor. It allows users to view connections, networked objects, message handlers, and packet statistics. This information can be helpful when debugging networked games.</para>
/// </summary>
[AddComponentMenu("Network/NetworkManagerHUD")]
[RequireComponent(typeof(NetworkManager))]
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkManagerHUD : MonoBehaviour
{
/// <summary>
/// The NetworkManager associated with this HUD.
/// </summary>
public NetworkManager manager;
/// <summary>
/// Whether to show the default control HUD at runtime.
/// </summary>
[SerializeField] public bool showGUI = true;
/// <summary>
/// The horizontal offset in pixels to draw the HUD runtime GUI at.
/// </summary>
[SerializeField] public int offsetX;
/// <summary>
/// The vertical offset in pixels to draw the HUD runtime GUI at.
/// </summary>
[SerializeField] public int offsetY;
// Runtime variable
bool m_ShowServer;
void Awake()
{
manager = GetComponent<NetworkManager>();
}
void Update()
{
if (!showGUI)
return;
if (!manager.IsClientConnected() && !NetworkServer.active && manager.matchMaker == null)
{
if (UnityEngine.Application.platform != RuntimePlatform.WebGLPlayer)
{
if (Input.GetKeyDown(KeyCode.S))
{
manager.StartServer();
}
if (Input.GetKeyDown(KeyCode.H))
{
manager.StartHost();
}
}
if (Input.GetKeyDown(KeyCode.C))
{
manager.StartClient();
}
}
if (NetworkServer.active)
{
if (manager.IsClientConnected())
{
if (Input.GetKeyDown(KeyCode.X))
{
manager.StopHost();
}
}
else
{
if (Input.GetKeyDown(KeyCode.X))
{
manager.StopServer();
}
}
}
}
void OnGUI()
{
if (!showGUI)
return;
int xpos = 10 + offsetX;
int ypos = 40 + offsetY;
const int spacing = 24;
bool noConnection = (manager.client == null || manager.client.connection == null ||
manager.client.connection.connectionId == -1);
if (!manager.IsClientConnected() && !NetworkServer.active && manager.matchMaker == null)
{
if (noConnection)
{
if (UnityEngine.Application.platform != RuntimePlatform.WebGLPlayer)
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Host(H)"))
{
manager.StartHost();
}
ypos += spacing;
}
if (GUI.Button(new Rect(xpos, ypos, 105, 20), "LAN Client(C)"))
{
manager.StartClient();
}
manager.networkAddress = GUI.TextField(new Rect(xpos + 100, ypos, 95, 20), manager.networkAddress);
ypos += spacing;
if (UnityEngine.Application.platform == RuntimePlatform.WebGLPlayer)
{
// cant be a server in webgl build
GUI.Box(new Rect(xpos, ypos, 200, 25), "( WebGL cannot be server )");
ypos += spacing;
}
else
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "LAN Server Only(S)"))
{
manager.StartServer();
}
ypos += spacing;
}
}
else
{
GUI.Label(new Rect(xpos, ypos, 200, 20), "Connecting to " + manager.networkAddress + ":" + manager.networkPort + "..");
ypos += spacing;
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Cancel Connection Attempt"))
{
manager.StopClient();
}
}
}
else
{
if (NetworkServer.active)
{
string serverMsg = "Server: port=" + manager.networkPort;
if (manager.useWebSockets)
{
serverMsg += " (Using WebSockets)";
}
GUI.Label(new Rect(xpos, ypos, 300, 20), serverMsg);
ypos += spacing;
}
if (manager.IsClientConnected())
{
GUI.Label(new Rect(xpos, ypos, 300, 20), "Client: address=" + manager.networkAddress + " port=" + manager.networkPort);
ypos += spacing;
}
}
if (manager.IsClientConnected() && !ClientScene.ready)
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Client Ready"))
{
ClientScene.Ready(manager.client.connection);
if (ClientScene.localPlayers.Count == 0)
{
ClientScene.AddPlayer(0);
}
}
ypos += spacing;
}
if (NetworkServer.active || manager.IsClientConnected())
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Stop (X)"))
{
manager.StopHost();
}
ypos += spacing;
}
if (!NetworkServer.active && !manager.IsClientConnected() && noConnection)
{
ypos += 10;
if (UnityEngine.Application.platform == RuntimePlatform.WebGLPlayer)
{
GUI.Box(new Rect(xpos - 5, ypos, 220, 25), "(WebGL cannot use Match Maker)");
return;
}
if (manager.matchMaker == null)
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Enable Match Maker (M)"))
{
manager.StartMatchMaker();
}
ypos += spacing;
}
else
{
if (manager.matchInfo == null)
{
if (manager.matches == null)
{
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Create Internet Match"))
{
manager.matchMaker.CreateMatch(manager.matchName, manager.matchSize, true, "", "", "", 0, 0, manager.OnMatchCreate);
}
ypos += spacing;
GUI.Label(new Rect(xpos, ypos, 100, 20), "Room Name:");
manager.matchName = GUI.TextField(new Rect(xpos + 100, ypos, 100, 20), manager.matchName);
ypos += spacing;
ypos += 10;
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Find Internet Match"))
{
manager.matchMaker.ListMatches(0, 20, "", false, 0, 0, manager.OnMatchList);
}
ypos += spacing;
}
else
{
for (int i = 0; i < manager.matches.Count; i++)
{
var match = manager.matches[i];
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Join Match:" + match.name))
{
manager.matchName = match.name;
manager.matchMaker.JoinMatch(match.networkId, "", "", "", 0, 0, manager.OnMatchJoined);
}
ypos += spacing;
}
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Back to Match Menu"))
{
manager.matches = null;
}
ypos += spacing;
}
}
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Change MM server"))
{
m_ShowServer = !m_ShowServer;
}
if (m_ShowServer)
{
ypos += spacing;
if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Local"))
{
manager.SetMatchHost("localhost", 1337, false);
m_ShowServer = false;
}
ypos += spacing;
if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Internet"))
{
manager.SetMatchHost("mm.unet.unity3d.com", 443, true);
m_ShowServer = false;
}
ypos += spacing;
if (GUI.Button(new Rect(xpos, ypos, 100, 20), "Staging"))
{
manager.SetMatchHost("staging-mm.unet.unity3d.com", 443, true);
m_ShowServer = false;
}
}
ypos += spacing;
GUI.Label(new Rect(xpos, ypos, 300, 20), "MM Uri: " + manager.matchMaker.baseUri);
ypos += spacing;
if (GUI.Button(new Rect(xpos, ypos, 200, 20), "Disable Match Maker"))
{
manager.StopMatchMaker();
}
ypos += spacing;
}
}
}
}
}