Unity获取CPU占有率

2023-11-20 16:59
文章标签 cpu 获取 unity 占有率

本文主要是介绍Unity获取CPU占有率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网上很多都是用PerformanceCounter,但是返回的总是0或者100

我在网上找到一个可行的,分享给大家。

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using TMPro;
using UnityEngine;public class DebugUIManager : MonoBehaviour
{[Header("Components")][SerializeField] private TMP_Text cpuCounterText;[Header("Settings")][Tooltip("In which interval should the CPU usage be updated?")][SerializeField] private float updateInterval = 1;[Tooltip("The amount of physical CPU cores")][SerializeField] private int processorCount;[Header("Output")]public float CpuUsage;private Thread _cpuThread;private float _lasCpuUsage;private void Start(){Application.runInBackground = true;cpuCounterText.text = "0% CPU";// setup the thread_cpuThread = new Thread(UpdateCPUUsage){IsBackground = true,// we don't want that our measurement thread// steals performancePriority = System.Threading.ThreadPriority.BelowNormal};// start the cpu usage thread_cpuThread.Start();}private void OnValidate(){// We want only the physical cores but usually// this returns the twice as many virtual core count//// if this returns a wrong value for you comment this method out// and set the value manuallyprocessorCount = SystemInfo.processorCount / 2;}private void OnDestroy(){// Just to be sure kill the thread if this object is destroyed_cpuThread?.Abort();}private void Update(){// for more efficiency skip if nothing has changedif (Mathf.Approximately(_lasCpuUsage, CpuUsage)) return;// the first two values will always be "wrong"// until _lastCpuTime is initialized correctly// so simply ignore values that are out of the possible rangeif (CpuUsage < 0 || CpuUsage > 100) return;// I used a float instead of int for the % so use the ToString you like for displaying itcpuCounterText.text = CpuUsage.ToString("F1") + "% CPU";// Update the value of _lasCpuUsage_lasCpuUsage = CpuUsage;}/// <summary>/// Runs in Thread/// </summary>private void UpdateCPUUsage(){var lastCpuTime = new TimeSpan(0);// This is ok since this is executed in a background threadwhile (true){var cpuTime = new TimeSpan(0);// Get a list of all running processes in this PCvar AllProcesses = Process.GetProcesses();// Sum up the total processor time of all running processescpuTime = AllProcesses.Aggregate(cpuTime, (current, process) => current + process.TotalProcessorTime);// get the difference between the total sum of processor times// and the last time we called thisvar newCPUTime = cpuTime - lastCpuTime;// update the value of _lastCpuTimelastCpuTime = cpuTime;// The value we look for is the difference, so the processor time all processes together used// since the last time we called this divided by the time we waited// Then since the performance was optionally spread equally over all physical CPUs// we also divide by the physical CPU countCpuUsage = 100f * (float)newCPUTime.TotalSeconds / updateInterval / processorCount;// Wait for UpdateIntervalThread.Sleep(Mathf.RoundToInt(updateInterval * 1000));}}
}

原文章地址 https://stackoverflow.com/questions/56684306/how-to-read-system-usage-cpu-ram-etc

这篇关于Unity获取CPU占有率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/396081

相关文章

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,