This returns 0 on 1st frame and 100 on every other. is there other way to get CPU usage in script?
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class PerformanceCheck : MonoBehaviour
{
System.Diagnostics.PerformanceCounter cpuCounter;
void Start()
{
System.Diagnostics.PerformanceCounterCategory.Exists("PerformanceCounter");
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", Process.GetCurrentProcess().ProcessName);
/*cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";*/
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), "CPU: " + getCurrentCpuUsage());
}
public string getCurrentCpuUsage()
{
return cpuCounter.NextValue() + "%";
}
}