Skip to content

Instantly share code, notes, and snippets.

@HolyFot
Last active August 28, 2020 07:35
Show Gist options
  • Save HolyFot/d2c6a7d1663c39be141a2ed15a46956d to your computer and use it in GitHub Desktop.
Save HolyFot/d2c6a7d1663c39be141a2ed15a46956d to your computer and use it in GitHub Desktop.
FPS & KBs In and Out for Forge/Unity C#
//Displays: actual FPS, KBs In/Out for Forge, System Info
//Author: HolyFot
//License: CC0
using UnityEngine;
using System;
using System.Collections;
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
public class FPSNetStats : MonoBehaviour
{
public KeyCode openFPS = KeyCode.P;
public bool state;
private float deltaTime = 0.0f;
private float kbsTimer = 1.0f; //X Seconds
private float currTimer;
private float kbInLast = 0.0f;
private float kbOutLast = 0.0f;
private float kbInCurr = 0.0f;
private float kbOutCurr = 0.0f;
private float kbsIN1 = 0.0f;
private float kbsOUT1 = 0.0f;
private int fpsCount = 0;
public bool isEnabled = false;
private int currentFPS = 0;
BMSLogger loggerz;
void Awake()
{
loggerz = FindObjectOfType<BMSLogger>();
}
void OnEnable()
{
isEnabled = true;
StartCoroutine("FPSCalc");
}
void OnDisable()
{
isEnabled = false;
StopCoroutine("FPSCalc");
}
void FixedUpdate()
{
//Update FPS (Not 100% accurate)
//deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
//Update KBs Timer
currTimer -= Time.deltaTime;
if (currTimer <= 0.0f) //1 Second
{
kbsIN1 = 0.0f;
kbsOUT1 = 0.0f;
kbInCurr = 0.0f;
kbOutCurr = 0.0f;
kbInCurr = (NetworkManager.Instance.Networker.BandwidthIn / 1024.0f);
kbOutCurr = (NetworkManager.Instance.Networker.BandwidthOut / 1024.0f);
if (kbInCurr - kbInLast > 0.0f)
kbsIN1 = (kbInCurr - kbInLast);
if (kbOutCurr - kbOutLast > 0.0f)
kbsOUT1 = (kbOutCurr - kbOutLast);
kbInLast = 0.0f;
kbOutLast = 0.0f;
kbInLast = (NetworkManager.Instance.Networker.BandwidthIn / 1024.0f);
kbOutLast = (NetworkManager.Instance.Networker.BandwidthOut / 1024.0f);
//Calculate Accurate FPS & Reset
currentFPS = fpsCount;
fpsCount = 0;
currTimer = kbsTimer; //Reset Timer
}
if (Input.GetKeyDown(openFPS))
{
if (state == false) //Toggle
{
if (loggerz != null)
loggerz.enabled = true;
state = true;
}
else
{
if (loggerz != null)
loggerz.enabled = false;
state = false;
}
}
}
private void OnGUI()
{
if (state)
{
//CALC KBS IN & OUT
string kbsIN = string.Format("{0:0} KB/s", kbsIN1);
string kbsOUT = string.Format("{0:0} KB/s", kbsOUT1);
GUILayout.Space(45);
//GUILayout.Label("The current server time is: " + string.Format("{0:0}", NetworkManager.Instance.Networker.Time));
GUILayout.Label("Kilobytes In: " + kbsIN);
GUILayout.Label("Kilobytes Out: " + kbsOUT);
//CALC FPS & UPDATE TIME
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, currentFPS); //fps*2
GUILayout.Label("FPS: " + text);
//Ping
//GUILayout.Space(15);
//GUILayout.Label("Ping: " + bl_Utilz.SetGetPingGameServer);
//Monitor Info
GUILayout.Label("Resolution: " + Screen.width + "x" + Screen.height + ": " + Screen.currentResolution.refreshRate + " Hz");
//Hardware Info
GUILayout.Label("CPU: " + SystemInfo.processorType);
GUILayout.Label("Memory: " + FormatMem(SystemInfo.systemMemorySize));
GUILayout.Label("GFX Card: " + SystemInfo.graphicsDeviceName);
}
}
IEnumerator FPSCalc()
{
while (isEnabled)
{
yield return 0; //Count Each Frame
fpsCount++;
}
}
private string FormatMem(long num)
{
long i = (long)Math.Pow(10, (int)Math.Max(0, Math.Log10(num) - 2));
num = num / i * i;
if (num >= 1000)
return (num / 1000D).ToString("0.##") + "GB";
return num.ToString("#,0") + "MB";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment