4
\$\begingroup\$

In Unity 5 I would like to be able to print out the CPU Usage and Available Ram left in MB. I followed the code posted on http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html and used it, however my CPU usage is always 100 and available ram is 0. According to the windows task manager CPU usage should be around 15-20% and available memory is around 4700MB. This is my first time using performance counters and further searching gives me the same code as below (from link):

public class ClientValues : MonoBehaviour {

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
public float myCPU;
public float myRAM;

// Use this for initialization
void Start () {

    cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    ramCounter = new PerformanceCounter("Memory", "Available MBytes");
}

// Update is called once per frame
void Update () 
{
    myCPU = getCurrentCpuUsage ();
    myRAM = getAvailableRAM ();
    //UnityEngine.Debug.Log (myCPU);
}

public float getCurrentCpuUsage(){
    return cpuCounter.NextValue();
}

public float getAvailableRAM(){
    return ramCounter.NextValue();
} 
}

What needs to change for this to give me the correct values?

\$\endgroup\$
  • \$\begingroup\$ Did you try googling? answers.unity3d.com/questions/506736/… \$\endgroup\$ – Den May 14 '15 at 11:37
  • \$\begingroup\$ Yea I seen this and tried it aswell, changing to .Net 2.0 Subset gives an error, the type or namespace PerformanceCounter does not exist. So its either an error or 100 cpu and 0mb ram \$\endgroup\$ – CH99 May 14 '15 at 11:49
  • \$\begingroup\$ Yes, Unity3d is weird like that. I am not even entirely sure that it uses actual C# and .NET/Mono. Depending on your goals this might be a better option: monogame.net \$\endgroup\$ – Den May 14 '15 at 21:43
  • \$\begingroup\$ Maybe this? bugzilla.xamarin.com/show_bug.cgi?id=1772 \$\endgroup\$ – Den May 14 '15 at 21:45
  • \$\begingroup\$ Again thanks for the replies Den, with Monogame.net would that not mean using visual studio instead of Unity? Also with the second link for the bug, with my understanding comparing to my code snippet above I would just need to change instance name from "_total" to Process.GetCurrentProcess().Id.ToString(), am I right in saying that? Because that change gives me the same results :( \$\endgroup\$ – CH99 May 18 '15 at 10:24
-1
\$\begingroup\$

On Unity 5, you may use Profiler window

http://docs.unity3d.com/Manual/ProfilerWindow.html

\$\endgroup\$
  • \$\begingroup\$ Sorry selharxets, this isnt what I am looking for, if the exact numbers of the profiler could be output into code instead of the logfile then yes, but considering this cant be done I am trying to use the performance counters seen above, but to no avail. \$\endgroup\$ – CH99 May 18 '15 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.