Advertisement
HolyFot

FPS & NetStats & SysInfo for Forge/Unity C#

May 15th, 2020 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. //Displays: actual FPS, KBs In/Out for Forge, System Info
  2. //Author: HolyFot
  3. //License: CC0
  4. using UnityEngine;
  5. using System;
  6. using System.Collections;
  7. using BeardedManStudios.Forge.Networking;
  8. using BeardedManStudios.Forge.Networking.Unity;
  9.  
  10. public class FPSNetStats : MonoBehaviour
  11. {
  12. public KeyCode openFPS = KeyCode.P;
  13. public bool state;
  14.  
  15. private float deltaTime = 0.0f;
  16. private float kbsTimer = 1.0f; //X Seconds
  17. private float currTimer;
  18. private float kbInLast = 0.0f;
  19. private float kbOutLast = 0.0f;
  20. private float kbInCurr = 0.0f;
  21. private float kbOutCurr = 0.0f;
  22. private float kbsIN1 = 0.0f;
  23. private float kbsOUT1 = 0.0f;
  24.  
  25. private int fpsCount = 0;
  26. public bool isEnabled = false;
  27. private int currentFPS = 0;
  28. BMSLogger loggerz;
  29. void Awake()
  30. {
  31. loggerz = FindObjectOfType<BMSLogger>();
  32. }
  33.  
  34. void OnEnable()
  35. {
  36. isEnabled = true;
  37. StartCoroutine("FPSCalc");
  38. }
  39.  
  40. void OnDisable()
  41. {
  42. isEnabled = false;
  43. StopCoroutine("FPSCalc");
  44. }
  45.  
  46. void FixedUpdate()
  47. {
  48. //Update FPS (Not 100% accurate)
  49. //deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
  50.  
  51. //Update KBs Timer
  52. currTimer -= Time.deltaTime;
  53. if (currTimer <= 0.0f) //1 Second
  54. {
  55. kbsIN1 = 0.0f;
  56. kbsOUT1 = 0.0f;
  57. kbInCurr = 0.0f;
  58. kbOutCurr = 0.0f;
  59. kbInCurr = (NetworkManager.Instance.Networker.BandwidthIn / 1024.0f);
  60. kbOutCurr = (NetworkManager.Instance.Networker.BandwidthOut / 1024.0f);
  61. if (kbInCurr - kbInLast > 0.0f)
  62. kbsIN1 = (kbInCurr - kbInLast);
  63. if (kbOutCurr - kbOutLast > 0.0f)
  64. kbsOUT1 = (kbOutCurr - kbOutLast);
  65.  
  66. kbInLast = 0.0f;
  67. kbOutLast = 0.0f;
  68. kbInLast = (NetworkManager.Instance.Networker.BandwidthIn / 1024.0f);
  69. kbOutLast = (NetworkManager.Instance.Networker.BandwidthOut / 1024.0f);
  70.  
  71. //Calculate Accurate FPS & Reset
  72. currentFPS = fpsCount;
  73. fpsCount = 0;
  74.  
  75. currTimer = kbsTimer; //Reset Timer
  76. }
  77.  
  78. if (Input.GetKeyDown(openFPS))
  79. {
  80. if (state == false) //Toggle
  81. {
  82. if (loggerz != null)
  83. loggerz.enabled = true;
  84. state = true;
  85. }
  86. else
  87. {
  88. if (loggerz != null)
  89. loggerz.enabled = false;
  90. state = false;
  91. }
  92. }
  93. }
  94.  
  95. private void OnGUI()
  96. {
  97. if (state)
  98. {
  99. //CALC KBS IN & OUT
  100. string kbsIN = string.Format("{0:0} KB/s", kbsIN1);
  101. string kbsOUT = string.Format("{0:0} KB/s", kbsOUT1);
  102.  
  103. GUILayout.Space(45);
  104. //GUILayout.Label("The current server time is: " + string.Format("{0:0}", NetworkManager.Instance.Networker.Time));
  105. GUILayout.Label("Kilobytes In: " + kbsIN);
  106. GUILayout.Label("Kilobytes Out: " + kbsOUT);
  107.  
  108. //CALC FPS & UPDATE TIME
  109. float msec = deltaTime * 1000.0f;
  110. float fps = 1.0f / deltaTime;
  111. string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, currentFPS); //fps*2
  112. GUILayout.Label("FPS: " + text);
  113.  
  114. //Ping
  115. //GUILayout.Space(15);
  116. //GUILayout.Label("Ping: " + bl_Utilz.SetGetPingGameServer);
  117.  
  118. //Monitor Info
  119. GUILayout.Label("Resolution: " + Screen.width + "x" + Screen.height + ": " + Screen.currentResolution.refreshRate + " Hz");
  120.  
  121. //Hardware Info
  122. GUILayout.Label("CPU: " + SystemInfo.processorType);
  123. GUILayout.Label("Memory: " + FormatMem(SystemInfo.systemMemorySize));
  124. GUILayout.Label("GFX Card: " + SystemInfo.graphicsDeviceName);
  125. }
  126. }
  127.  
  128. IEnumerator FPSCalc()
  129. {
  130. while (isEnabled)
  131. {
  132. yield return 0; //Count Each Frame
  133. fpsCount++;
  134. }
  135. }
  136.  
  137. private string FormatMem(long num)
  138. {
  139. long i = (long)Math.Pow(10, (int)Math.Max(0, Math.Log10(num) - 2));
  140. num = num / i * i;
  141. if (num >= 1000)
  142. return (num / 1000D).ToString("0.##") + "GB";
  143.  
  144. return num.ToString("#,0") + "MB";
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement