When i use PerformanceCounter in unity, they return 0mb free RAM and %100 cpu usage. If i use same codes on Visual Studio project, it works. How can i allow this code to get system’s information in Unity? I need to get free RAM and CPU usage on the computer. Unity’s profiler doesn’t shows free memory. Here is the .dll file which i wrote.
using System;
using System.Diagnostics;
namespace MemCu
{
public class Profilers
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
string mem;
string cpu;
public string ProfileCpu()
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
cpu = Convert.ToString(cpuCounter.NextValue());
return cpu;
}
public string ProfileMem()
{
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
mem = Convert.ToString(ramCounter.NextValue());
return mem;
}
}
}
Could be due to Unity not being able to access the performance counters properly. You can try this:
Make sure you’re using the correct API Compatibility Level. In Unity, go to Edit > Project Settings > Player, then under Other Settings, find the API Compatibility Level and set it to .NET 4.x.
Ensure you have the necessary permissions to access performance counters. Running Unity as an administrator might help. Right-click on the Unity icon and select Run as administrator.
If the problem persists, you can try using an alternative approach to get system information. The System.Management namespace can be used to query WMI (Windows Management Instrumentation) for system information. However, the System.Management assembly is not available by default in Unity. You can follow these steps to import the required DLL into your Unity project:
a. Locate the System.Management.dll file on your computer. It’s usually in the C:\Windows\Microsoft.NET\Framework\v4.0.30319 folder (or a similar path).
b. Copy the System.Management.dll file into your Unity project’s Assets folder.
c. In Unity, you should now see the System.Management.dll in your Assets folder.
Now you can modify your code to use WMI queries to get CPU and RAM information. Here’s an example implementation:
using System;
using System.Management;
namespace MemCu
{
public class Profilers
{
public string ProfileCpu()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'");
double cpuUsage = 0;
foreach (ManagementObject obj in searcher.Get())
{
cpuUsage = Convert.ToDouble(obj["PercentProcessorTime"]);
}
return cpuUsage.ToString();
}
public string ProfileMem()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
double freeMemory = 0;
foreach (ManagementObject obj in searcher.Get())
{
freeMemory = Convert.ToDouble(obj["FreePhysicalMemory"]) / 1024; // Convert from KB to MB
}
return freeMemory.ToString();
}
}
}
Keep in mind that this solution is Windows-specific and will not work on other platforms.