Mirror/Unity-Technologies-networking/Runtime/NetworkManagerHUD.cs

170 lines
5.4 KiB
C#
Raw Normal View History

using System;
using System.ComponentModel;
#if ENABLE_UNET
namespace UnityEngine.Networking
{
[AddComponentMenu("Network/NetworkManagerHUD")]
[RequireComponent(typeof(NetworkManager))]
[EditorBrowsable(EditorBrowsableState.Never)]
public class NetworkManagerHUD : MonoBehaviour
{
public NetworkManager manager;
[SerializeField] public bool showGUI = true;
[SerializeField] public int offsetX;
[SerializeField] public int offsetY;
void Awake()
{
manager = GetComponent<NetworkManager>();
}
void Update()
{
if (!showGUI)
return;
if (!manager.IsClientConnected() && !NetworkServer.active)
{
if (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)
{
if (noConnection)
{
if (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 (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;
}
}
}
}
#endif //ENABLE_UNET