2018-06-07 13:41:08 +00:00
|
|
|
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;
|
|
|
|
|
2018-07-19 15:43:37 +00:00
|
|
|
if (!manager.IsClientConnected() && !NetworkServer.active)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
2018-07-19 10:01:46 +00:00
|
|
|
if (Application.platform != RuntimePlatform.WebGLPlayer)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2018-07-19 15:43:37 +00:00
|
|
|
if (!manager.IsClientConnected() && !NetworkServer.active)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
if (noConnection)
|
|
|
|
{
|
2018-07-19 10:01:46 +00:00
|
|
|
if (Application.platform != RuntimePlatform.WebGLPlayer)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2018-07-19 10:01:46 +00:00
|
|
|
if (Application.platform == RuntimePlatform.WebGLPlayer)
|
2018-06-07 13:41:08 +00:00
|
|
|
{
|
|
|
|
// 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
|