Created
March 16, 2021 21:01
-
-
Save James-Frowen/691c115bc8e8ea07a34d56b401e5b35a to your computer and use it in GitHub Desktop.
OnGui for NetworkManager to make iit quick to set up and start a server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Cysharp.Threading.Tasks; | |
using UnityEngine; | |
namespace Mirage | |
{ | |
/// <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> | |
[DisallowMultipleComponent] | |
[AddComponentMenu("Network/NetworkManagerHUD")] | |
[RequireComponent(typeof(NetworkManager))] | |
[HelpURL("https://mirror-networking.com/docs/Articles/Components/NetworkManagerHUD.html")] | |
public class NetworkManagerGUI : MonoBehaviour | |
{ | |
NetworkManager manager; | |
/// <summary> | |
/// Whether to show the default control HUD at runtime. | |
/// </summary> | |
public bool showGUI = true; | |
/// <summary> | |
/// The horizontal offset in pixels to draw the HUD runtime GUI at. | |
/// </summary> | |
public int offsetX; | |
/// <summary> | |
/// The vertical offset in pixels to draw the HUD runtime GUI at. | |
/// </summary> | |
public int offsetY; | |
private string networkAddress; | |
void Awake() | |
{ | |
manager = GetComponent<NetworkManager>(); | |
} | |
void OnGUI() | |
{ | |
if (!showGUI) | |
return; | |
GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999)); | |
if (!manager.Client.Active && !manager.Server.Active) | |
{ | |
StartButtons(); | |
} | |
else | |
{ | |
StatusLabels(); | |
} | |
StopButtons(); | |
GUILayout.EndArea(); | |
} | |
void StartButtons() | |
{ | |
if (!manager.Client.Active) | |
{ | |
// Server + Client | |
if (Application.platform != RuntimePlatform.WebGLPlayer) | |
{ | |
if (GUILayout.Button("Host (Server + Client)")) | |
{ | |
manager.Server.StartHost(manager.Client).Forget(); | |
} | |
} | |
// Client + IP | |
GUILayout.BeginHorizontal(); | |
if (GUILayout.Button("Client")) | |
{ | |
manager.Client.ConnectAsync(networkAddress).Forget(); | |
} | |
GUILayout.EndHorizontal(); | |
// Server Only | |
if (Application.platform == RuntimePlatform.WebGLPlayer) | |
{ | |
// cant be a server in webgl build | |
GUILayout.Box("( WebGL cannot be server )"); | |
} | |
else | |
{ | |
if (GUILayout.Button("Server Only")) | |
manager.Server.ListenAsync().Forget(); | |
} | |
} | |
else | |
{ | |
// Connecting | |
GUILayout.Label("Connecting to " + networkAddress + ".."); | |
if (GUILayout.Button("Cancel Connection Attempt")) | |
{ | |
manager.Client.Disconnect(); | |
} | |
} | |
} | |
void StatusLabels() | |
{ | |
// server / client status message | |
if (manager.Server.Active) | |
{ | |
GUILayout.Label("Server: active. Transport: " + manager.Server.Transport); | |
} | |
if (manager.Client.IsConnected) | |
{ | |
GUILayout.Label("Client: address=" + networkAddress); | |
} | |
} | |
void StopButtons() | |
{ | |
// stop host if host mode | |
if (manager.Server.Active && manager.Client.IsConnected) | |
{ | |
if (GUILayout.Button("Stop Host")) | |
{ | |
manager.Server.StopHost(); | |
} | |
} | |
// stop client if client-only | |
else if (manager.Client.IsConnected) | |
{ | |
if (GUILayout.Button("Stop Client")) | |
{ | |
manager.Client.Disconnect(); | |
} | |
} | |
// stop server if server-only | |
else if (manager.Server.Active) | |
{ | |
if (GUILayout.Button("Stop Server")) | |
{ | |
manager.Server.Disconnect(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment