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

Perform output
Console. WriteLine (" CCCCCCC "+ CpuLoad);

CodePudding user response:

reference 1st floor qq_26597393 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

Perform output
Console. WriteLine (" CCCCCCC "+ CpuLoad);


reference 1st floor qq_26597393 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

Perform output
Console. WriteLine (" CCCCCCC "+ CpuLoad);
I forgot the using System. The Diagnostics;

CodePudding user response:

Not do ah yes,, still has always been 100 ah, the return value,

CodePudding user response:

Or 100? I tried to use ah,,,

CodePudding user response:

Are you sure you are in unity with you,,,, can you send me a can of engineering,,,,

CodePudding user response:

What is the meaning of using the get statements? .

CodePudding user response:

It doesn't make any sense,,, used to use get,,,

CodePudding user response:

Okay, I give you write a

CodePudding user response:

W I wipe I wrote this API is not hung in the unity of the server

CodePudding user response:

So this thing ah, in the unity, and return to 100, all the time, don't know what reason,,, about mono is a virtual machine? ,,
The inside of the my last call kernel32 GetSystemTimes function is calculated to write their own code, CPU utilization,,,

But I'm still very curious why nextvalue always return 100,,,

CodePudding user response:

Be curious as to confusion really server can use the return value unity is not right

CodePudding user response:

By the way you put the knot of the posts I need score (shy face? (? ????? Omega??? ?)? )

CodePudding user response:

Alas, there, not happy,,
  • Related