NetworkPingDisplay.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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/NetworkPingDisplay")]
  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. int width = 150;
  16. int height = 25;
  17. void OnGUI()
  18. {
  19. // only while client is active
  20. if (!NetworkClient.active) return;
  21. // show rtt 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. GUIStyle style = GUI.skin.GetStyle("Label");
  25. style.alignment = TextAnchor.MiddleRight;
  26. GUI.Label(rect, $"RTT: {Math.Round(NetworkTime.rtt * 1000)}ms", style);
  27. GUI.color = Color.white;
  28. }
  29. }
  30. }