Skip to content

Commit

Permalink
feat: adding script that displays ping (#1975)
Browse files Browse the repository at this point in the history
* adding script that displays ping

* fixing style

* adding mirror icon

* Update Assets/Mirror/Components/NetworkPingDisplay.cs

Co-authored-by: MrGadget <chris@clevertech.net>

* adding DisallowMultipleComponent

* adding docs on NetworkPingDisplay

Co-authored-by: MrGadget <chris@clevertech.net>
  • Loading branch information
James-Frowen and MrGadget committed Jun 10, 2020
1 parent 3db57e5 commit 7e93030
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Assets/Mirror/Components/NetworkPingDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using UnityEngine;

namespace Mirror
{
/// <summary>
/// Component that will display the clients ping in milliseconds
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkPingDisplay")]
[HelpURL("https://mirror-networking.com/docs/Components/NetworkPingDisplay.html")]
public class NetworkPingDisplay : MonoBehaviour
{
[SerializeField] bool showPing = true;
[SerializeField] Vector2 position = new Vector2(200, 0);
[SerializeField] int fontSize = 24;
[SerializeField] Color textColor = new Color32(255, 255, 255, 80);

GUIStyle style;

void Awake()
{
style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
style.fontSize = fontSize;
style.normal.textColor = textColor;
}

void OnGUI()
{
if (!showPing) { return; }

string text = string.Format("{0}ms", (int)(NetworkTime.rtt * 1000));

int width = Screen.width;
int height = Screen.height;
Rect rect = new Rect(position.x, position.y, width - 200, height * 2 / 100);

GUI.Label(rect, text, style);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Components/NetworkPingDisplay.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions doc/Components/NetworkPingDisplay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Network Ping Display

Network Ping Display shows the Ping time for clients using OnGUI.

The Ping time is the moving average of the of the (RTT) Round-trip delay time. RTT is calculated by the PingMessage/PongMessage between the client and server.

![Inspector](NetworkPingDisplay.png)

See [Clock Synchronization](../Guides/ClockSync.md) for more info.
Binary file added doc/Components/NetworkPingDisplay.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions doc/Components/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ These core components are included in Mirror:
Network Authenticators facilitate integration of user accounts and credentials into your application.
- [Network Discovery](NetworkDiscovery.md)
Network Discovery uses a UDP broadcast on the LAN enabling clients to find the running server and connect to it.
- [Network Ping Display](NetworkPingDisplay.md)
Network Ping Display shows the Ping time for clients using OnGUI
- [Network Identity](NetworkIdentity.md)
The Network Identity component is at the heart of the Mirror networking high-level API. It controls a game object’s unique identity on the network, and it uses that identity to make the networking system aware of the game object. It offers two different options for configuration and they are mutually exclusive, which means either one of the options or none can be checked.
- [Network Log Settings](NetworkLogSettings.md)
Expand Down

0 comments on commit 7e93030

Please sign in to comment.