In unity3d I'm trying to read the system usage in order to see the CPU and RAM usage whilst a person is playing a VR game. I know how I can show it on the computer screen and how to keep it away from the players VR screen.
I have been looking all around Google for as far as I know. Most of them lead to the same answer which I cannot seem to get working correctly.
I have been checking out the post: https://answers.unity.com/questions/506736/measure-cpu-and-memory-load-in-code.html which leads me to the stackoverflow page: How to get the CPU Usage in C#?
In these posts they always get the problem of having the CPU usage stuck at 100%, even though this is incorrect. I have been trying the System.Threading.Thread.Sleep(1000). But this locks the entire game to 1fps due to it waiting 1 sec every second. And even with having 1fps I cannot get any other read than 100%.
In this code I'm using the Sleep thread which slows down the game to 1fps
using UnityEngine;
using System.Diagnostics;
using TMPro;
public class DebugUIManager : MonoBehaviour
{
private PerformanceCounter cpuCounter;
[SerializeField] private TMP_Text cpuCounterText;
private void Start()
{
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
}
private void Update()
{
cpuCounterText.text = getCurrentCpuUsage();
}
private string getCurrentCpuUsage()
{
dynamic firstValue = cpuCounter.NextValue() + "% CPU";
System.Threading.Thread.Sleep(1000);
dynamic secondValue = cpuCounter.NextValue() + "% CPU";
return secondValue;
}
}
Whenever I change the
private string getCurrentCpuUsage()
{
dynamic firstValue = cpuCounter.NextValue() + "% CPU";
System.Threading.Thread.Sleep(1000);
dynamic secondValue = cpuCounter.NextValue() + "% CPU";
return secondValue;
}
to
private string getCurrentCpuUsage()
{
return cpuCounter.NextValue() + "%";
}
the game won't get dropped in fps, but it also doesn't do anything still.
I haven't been getting any error messages because it runs without issues. But I would really like to know how to get the CPU usage, so I can figure out the rest myself.
Any answers helping would be appreciated.