Skip to content

Commit

Permalink
feat: New NetworkManagerHud (#340)
Browse files Browse the repository at this point in the history
* feat: new NetworkManagerHud

* remove old test and add new test

* add prefabs and supporting common scripts

* update example scenes to use new prefabs

* fix smells and test
  • Loading branch information
uweeby committed Aug 31, 2020
1 parent a499a37 commit 267f513
Show file tree
Hide file tree
Showing 26 changed files with 3,611 additions and 422 deletions.
160 changes: 32 additions & 128 deletions Assets/Mirror/Runtime/NetworkManagerHUD.cs
Original file line number Diff line number Diff line change
@@ -1,154 +1,58 @@
// vis2k: GUILayout instead of spacey += ...; removed Update hotkeys to avoid
// confusion if someone accidentally presses one.
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

namespace Mirror
{
/// <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))]
[EditorBrowsable(EditorBrowsableState.Never)]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkManagerHUD.html")]
public class NetworkManagerHUD : MonoBehaviour
{
NetworkManager manager;
public NetworkManager NetworkManager;
public string NetworkAddress = "localhost";

/// <summary>
/// Whether to show the default control HUD at runtime.
/// </summary>
public bool showGUI = true;
[Header("Prefab Canvas Elements")]
public InputField NetworkAddressInput;
public GameObject OfflineGO;
public GameObject OnlineGO;
public Text StatusLabel;

/// <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;

/// <summary>
/// The IP address we're connecting to.
/// </summary>
public string serverIp = "localhost";

void Awake()
internal void OnlineSetActive()
{
manager = GetComponent<NetworkManager>();
OfflineGO.SetActive(false);
OnlineGO.SetActive(true);
}

void OnGUI()
internal void OfflineSetActive()
{
if (!showGUI)
return;

GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999));

if (!manager.client.IsConnected && !manager.server.Active)
{
StartButtons();
}
else
{
StatusLabels();
StopButtons();
}

GUILayout.EndArea();
OfflineGO.SetActive(true);
OnlineGO.SetActive(false);
}

void StartButtons()
public void StartHostButtonHandler()
{
if (!manager.client.Active)
{
// Server + Client
if (Application.platform != RuntimePlatform.WebGLPlayer)
{
if (GUILayout.Button("Host (Server + Client)"))
{
_ = manager.StartHost();
}
}
_ = NetworkManager.StartHost();
OnlineSetActive();
}

// Client + IP
GUILayout.BeginHorizontal();
if (GUILayout.Button("Client"))
{
manager.StartClient(serverIp);
}
serverIp = GUILayout.TextField(serverIp);
GUILayout.EndHorizontal();
public void StartServerOnlyButtonHandler()
{
_ = NetworkManager.StartServer();
OnlineSetActive();
}

// 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.StartServer();
}
}
}
else
{
// Connecting
GUILayout.Label("Connecting to " + serverIp + "..");
if (GUILayout.Button("Cancel Connection Attempt"))
{
manager.StopClient();
}
}
public void StartClientButtonHandler()
{
_ = NetworkManager.StartClient(NetworkAddress);
OnlineSetActive();
}

void StatusLabels()
public void StopButtonHandler()
{
// server / client status message
if (manager.server.Active)
{
GUILayout.Label("Server: active. Transport: " + manager.server.transport);
}
if (manager.client.IsConnected)
{
GUILayout.Label("Client: address=" + serverIp);
}
NetworkManager.StopHost();
OfflineSetActive();
}

void StopButtons()
public void OnNetworkAddressInputUpdate()
{
// stop host if host mode
if (manager.server.Active && manager.client.IsConnected)
{
if (GUILayout.Button("Stop Host"))
{
manager.StopHost();
}
}
// stop client if client-only
else if (manager.client.IsConnected)
{
if (GUILayout.Button("Stop Client"))
{
manager.StopClient();
}
}
// stop server if server-only
else if (manager.server.Active)
{
if (GUILayout.Button("Stop Server"))
{
manager.StopServer();
}
}
NetworkAddress = NetworkAddressInput.text;
}
}
}
133 changes: 126 additions & 7 deletions Assets/Mirror/Samples~/AdditiveScenes/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ MonoBehaviour:
m_GameObject: {fileID: 534669902}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fc6b510c425006f458e9b99cb817c55b, type: 3}
m_Script: {fileID: 11500000, guid: 0ca4e0a7ae009604c80ea999df8324ba, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &589935540
Expand Down Expand Up @@ -1741,6 +1741,125 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1462312433}
m_Mesh: {fileID: 10206, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1001 &1606471218
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_RootOrder
value: 9
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchoredPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchoredPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_SizeDelta.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_SizeDelta.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchorMin.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchorMax.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_Pivot.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3485129733779382408, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_Pivot.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6722741378053637293, guid: 8fe65c0d72451f04584fcdd021b46308,
type: 3}
propertyPath: m_Name
value: NetworkManagerHudInCanvas
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 8fe65c0d72451f04584fcdd021b46308, type: 3}
--- !u!1 &1608696204
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1890,8 +2009,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 34d1daf9e7dbcb64aa647cb332054ea6, type: 3}
m_Name:
m_EditorClassIdentifier:
startOnHeadless: 1
serverTickRate: 30
server: {fileID: 1661834283}
client: {fileID: 1661834282}
OnStartHost:
Expand Down Expand Up @@ -1952,10 +2069,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 6442dc8070ceb41f094e44de0bf87274, type: 3}
m_Name:
m_EditorClassIdentifier:
showGUI: 1
offsetX: 0
offsetY: 0
serverIp: localhost
NetworkManager: {fileID: 0}
NetworkAddress: localhost
NetworkAddressInput: {fileID: 0}
OfflineGO: {fileID: 0}
OnlineGO: {fileID: 0}
StatusLabel: {fileID: 0}
--- !u!114 &1661834282
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 267f513

Please sign in to comment.