NetworkPingDisplay.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using UnityEngine;
  3. namespace Mirror
  4. {
  5. /// <summary>
  6. /// Component that will display the clients ping in milliseconds
  7. /// </summary>
  8. [DisallowMultipleComponent]
  9. [AddComponentMenu("Network/Network Ping Display")]
  10. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-ping-display")]
  11. public class NetworkPingDisplay : MonoBehaviour
  12. {
  13. public Color color = Color.white;
  14. public int padding = 2;
  15. public int width = 150;
  16. public int height = 25;
  17. void OnGUI()
  18. {
  19. // only while client is active
  20. if (!NetworkClient.active) return;
  21. // show stats in bottom right corner, right aligned
  22. GUI.color = color;
  23. Rect rect = new Rect(Screen.width - width - padding, Screen.height - height - padding, width, height);
  24. GUILayout.BeginArea(rect);
  25. GUIStyle style = GUI.skin.GetStyle("Label");
  26. style.alignment = TextAnchor.MiddleRight;
  27. GUILayout.BeginHorizontal(style);
  28. GUILayout.Label($"RTT: {Math.Round(NetworkTime.rtt * 1000)}ms");
  29. GUI.color = NetworkClient.connectionQuality.ColorCode();
  30. GUILayout.Label($"Q: {new string('-', (int)NetworkClient.connectionQuality)}");
  31. GUILayout.EndHorizontal();
  32. GUILayout.EndArea();
  33. GUI.color = Color.white;
  34. }
  35. }
  36. }