quantum-space-buddies/QuantumUNET/Components/QSBNetworkManagerHUD.cs

98 lines
2.4 KiB
C#
Raw Normal View History

2020-12-07 20:49:51 +00:00
using UnityEngine;
2020-12-02 18:40:38 +00:00
2020-12-07 21:19:16 +00:00
namespace QuantumUNET.Components
2020-12-02 18:40:38 +00:00
{
public class QSBNetworkManagerHUD : MonoBehaviour
{
2020-12-08 09:03:10 +00:00
public QSBNetworkManagerUNET manager;
public bool showGUI = true;
public int offsetX;
public int offsetY;
private void Awake() => manager = GetComponent<QSBNetworkManagerUNET>();
2020-12-02 18:40:38 +00:00
private void OnGUI()
{
2020-12-07 20:47:07 +00:00
if (showGUI)
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
var num = 10 + offsetX;
var num2 = 40 + offsetY;
var flag = manager.client == null || manager.client.connection == null || manager.client.connection.connectionId == -1;
if (!manager.IsClientConnected() && !QSBNetworkServer.active)
2020-12-02 18:40:38 +00:00
{
if (flag)
{
if (Application.platform != RuntimePlatform.WebGLPlayer)
{
2020-12-07 20:47:07 +00:00
if (GUI.Button(new Rect(num, num2, 200f, 20f), "Host"))
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
manager.StartHost();
2020-12-02 18:40:38 +00:00
}
num2 += 24;
}
2020-12-07 20:47:07 +00:00
if (GUI.Button(new Rect(num, num2, 105f, 20f), "Connect"))
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
manager.StartClient();
2020-12-02 18:40:38 +00:00
}
2020-12-07 20:47:07 +00:00
manager.networkAddress = GUI.TextField(new Rect(num + 100, num2, 95f, 20f), manager.networkAddress);
2020-12-02 18:40:38 +00:00
num2 += 24;
}
else
{
2020-12-07 20:47:07 +00:00
GUI.Label(new Rect(num, num2, 200f, 20f), string.Concat(new object[]
2020-12-02 18:40:38 +00:00
{
"Connecting to ",
2020-12-07 20:47:07 +00:00
manager.networkAddress,
2020-12-02 18:40:38 +00:00
":",
2020-12-07 20:47:07 +00:00
manager.networkPort,
2020-12-02 18:40:38 +00:00
".."
}));
num2 += 24;
2020-12-07 20:47:07 +00:00
if (GUI.Button(new Rect(num, num2, 200f, 20f), "Cancel Connection Attempt"))
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
manager.StopClient();
2020-12-02 18:40:38 +00:00
}
}
}
else
{
if (QSBNetworkServer.active)
{
2020-12-07 20:47:07 +00:00
var text = $"Hosting on port {manager.networkPort}";
if (manager.useWebSockets)
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
text += " (using WebSockets)";
2020-12-02 18:40:38 +00:00
}
2020-12-07 20:47:07 +00:00
GUI.Label(new Rect(num, num2, 300f, 20f), text);
2020-12-02 18:40:38 +00:00
num2 += 24;
}
2020-12-07 20:47:07 +00:00
if (manager.IsClientConnected())
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
GUI.Label(new Rect(num, num2, 300f, 20f), $"Connected to {manager.networkAddress}, port {manager.networkPort}");
2020-12-02 18:40:38 +00:00
num2 += 24;
}
}
2020-12-07 20:47:07 +00:00
if (manager.IsClientConnected() && !QSBClientScene.ready)
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
if (GUI.Button(new Rect(num, num2, 200f, 20f), "Client Ready"))
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
QSBClientScene.Ready(manager.client.connection);
2020-12-02 18:40:38 +00:00
if (QSBClientScene.localPlayers.Count == 0)
{
QSBClientScene.AddPlayer(0);
}
}
num2 += 24;
}
2020-12-07 20:47:07 +00:00
if (QSBNetworkServer.active || manager.IsClientConnected())
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
if (GUI.Button(new Rect(num, num2, 200f, 20f), "Stop"))
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
manager.StopHost();
2020-12-02 18:40:38 +00:00
}
num2 += 24;
}
}
}
}
2020-12-03 08:28:05 +00:00
}