quakeball/Assets/Scripts/Interface/NetDebugScreen.cs

51 lines
1.9 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
using NeonTea.Quakeball.Networking;
using System;
public class NetDebugScreen : MonoBehaviour {
public RectTransform BackgroundPanel;
public TMP_Text ReceivedText;
public TMP_Text SentText;
public float UpdateInterval = 5f;
private InputAction ToggleNetDebug;
private float LastUpdate;
private bool IsOpen;
private void Start() {
ToggleNetDebug = new InputAction("Toggle Net Debug", binding: "<Keyboard>/F3");
ToggleNetDebug.Enable();
ToggleNetDebug.performed += _ => {
IsOpen = !IsOpen;
BackgroundPanel.gameObject.SetActive(IsOpen);
};
}
private void Update() {
bool NetworkingAvailable = Net.Singleton.Instance != null && Net.Singleton.Instance.Peer.Running;
if (IsOpen && !NetworkingAvailable) {
ReceivedText.text = "No networking data to show";
SentText.text = "";
} else if (IsOpen && NetworkingAvailable) {
var Peer = Net.Singleton.Instance.Peer;
if (Time.time - LastUpdate > UpdateInterval) {
LastUpdate = Time.time;
float ToSeconds = 1f / (Peer.TrafficDataInterval / 1000f);
string received = $"Total Bytes Receivd: {Peer.TrafficReceived * ToSeconds} B/s\nReceived by packet:";
foreach (Type t in Peer.TrafficReceivedByPacket.Keys) {
received += $"\n{t.Name} - {Peer.TrafficReceivedByPacket[t] * ToSeconds} B/s";
}
ReceivedText.text = received;
string sent = $"Total Bytes Sent: {Net.Singleton.Instance.Peer.TrafficSent * ToSeconds} B/s\nSent by packet:";
foreach (Type t in Peer.TrafficSentByPacket.Keys) {
sent += $"\n{t.Name} - {Peer.TrafficSentByPacket[t] * ToSeconds} B/s";
}
SentText.text = sent;
}
}
}
}