Home > other > How to add a CPU detector made the unity game?
How to add a CPU detector made the unity game?
Time:09-22
My brain to a hot today, I suddenly want to make a current can be detected in the game the function of the computer CPU usage, I know it sounds stupid, so please don't waste time on this point, First of all, I cited several necessary libraries: Using UnityEngine; using System.Collections; using System.Text; using System; Using System. The Runtime. InteropServices; Using System. Diagnostics; Using System. The Threading;
Then run the environment Settings for. Net2.0, and then use the following code to apply for the class a global CPU monitor PerformanceCounter PC=new PerformanceCounter (" Processor ", "% Processor Time", "_Total");
Call PerformanceCounter class nextvalue function for CPU utilization value and output to the unity console:
Private void PerformanceCounterFun () { CpuLoad=PC. NextValue (); UnityEngine. Debug. The Log (" CPU load="+ cpuLoad +" %. "); UnityEngine. Debug. The Log (PC. NextValue ()); }
And then set its every 1.5 seconds once:
IEnumerator startCount () { While (true) { Yield return new WaitForSeconds (1.5 f); PerformanceCounterFun (); } }
Actual running up found, however, in addition to the first count is zero, the back of the CPU usage count of 100, and the resource manager's shows values less than 20%,,, I try to create a c # console application to do the same function, can output more accurate value, therefore the API itself should be yes, I am very curious why I'll get 100 this value in UNITY,,,,,,, My small white, the great god,,,,,,,
CodePudding user response:
Please _ please said occupancy rate this thing run slower Let dry make you Private PerformanceCounter pcCpuLoad=new PerformanceCounter (" Processor ", "% Processor Time", "_Total"); # region API //access to CPU usage Public float CpuLoad { The get { Return pcCpuLoad. NextValue (); } } # endregion