Mirror Networking
NetworkManagerHUD.cs
1// vis2k: GUILayout instead of spacey += ...; removed Update hotkeys to avoid
2// confusion if someone accidentally presses one.
3using UnityEngine;
4
5namespace Mirror
6{
8 [DisallowMultipleComponent]
9 [AddComponentMenu("Network/Network Manager HUD")]
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
16 public int offsetX;
17 public int offsetY;
18
19 void Awake()
20 {
21 manager = GetComponent<NetworkManager>();
22 }
23
24 void OnGUI()
25 {
26 GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999));
28 {
29 StartButtons();
30 }
31 else
32 {
33 StatusLabels();
34 }
35
36 // client ready
38 {
39 if (GUILayout.Button("Client Ready"))
40 {
42 if (NetworkClient.localPlayer == null)
43 {
45 }
46 }
47 }
48
49 StopButtons();
50
51 GUILayout.EndArea();
52 }
53
54 void StartButtons()
55 {
57 {
58 // Server + Client
59 if (Application.platform != RuntimePlatform.WebGLPlayer)
60 {
61 if (GUILayout.Button("Host (Server + Client)"))
62 {
63 manager.StartHost();
64 }
65 }
66
67 // Client + IP
68 GUILayout.BeginHorizontal();
69 if (GUILayout.Button("Client"))
70 {
71 manager.StartClient();
72 }
73 // This updates networkAddress every frame from the TextField
74 manager.networkAddress = GUILayout.TextField(manager.networkAddress);
75 GUILayout.EndHorizontal();
76
77 // Server Only
78 if (Application.platform == RuntimePlatform.WebGLPlayer)
79 {
80 // cant be a server in webgl build
81 GUILayout.Box("( WebGL cannot be server )");
82 }
83 else
84 {
85 if (GUILayout.Button("Server Only")) manager.StartServer();
86 }
87 }
88 else
89 {
90 // Connecting
91 GUILayout.Label($"Connecting to {manager.networkAddress}..");
92 if (GUILayout.Button("Cancel Connection Attempt"))
93 {
94 manager.StopClient();
95 }
96 }
97 }
98
99 void StatusLabels()
100 {
101 // host mode
102 // display separately because this always confused people:
103 // Server: ...
104 // Client: ...
106 {
107 GUILayout.Label($"<b>Host</b>: running via {Transport.activeTransport}");
108 }
109 // server only
110 else if (NetworkServer.active)
111 {
112 GUILayout.Label($"<b>Server</b>: running via {Transport.activeTransport}");
113 }
114 // client only
115 else if (NetworkClient.isConnected)
116 {
117 GUILayout.Label($"<b>Client</b>: connected to {manager.networkAddress} via {Transport.activeTransport}");
118 }
119 }
120
121 void StopButtons()
122 {
123 // stop host if host mode
125 {
126 if (GUILayout.Button("Stop Host"))
127 {
128 manager.StopHost();
129 }
130 }
131 // stop client if client-only
132 else if (NetworkClient.isConnected)
133 {
134 if (GUILayout.Button("Stop Client"))
135 {
136 manager.StopClient();
137 }
138 }
139 // stop server if server-only
140 else if (NetworkServer.active)
141 {
142 if (GUILayout.Button("Stop Server"))
143 {
144 manager.StopServer();
145 }
146 }
147 }
148 }
149}
NetworkClient with connection to server.
static bool active
active is true while a client is connecting/connected
static bool ready
True if client is ready (= joined world).
static bool Ready()
Sends Ready message to server, indicating that we loaded the scene, ready to enter the game.
static bool AddPlayer()
Sends AddPlayer message to the server, indicating that we want to join the world.
static bool isConnected
Check if client is connected (after connecting).
static NetworkIdentity localPlayer
NetworkIdentity of the localPlayer
Shows NetworkManager controls in a GUI at runtime.
void StartClient()
Starts the client, connects it to the server with networkAddress.
void StopHost()
This stops both the client and the server that the manager is using.
void StopServer()
Stops the server from listening and simulating the game.
void StartHost()
Starts a network "host" - a server and client in the same application.
string networkAddress
Server's address for clients to connect to.
void StopClient()
Stops and disconnects the client.
void StartServer()
Starts the server, listening for incoming connections.
NetworkServer handles remote connections and has a local connection for a local client.
static bool active
active checks if the server has been started