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

相关文章

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java通过反射获取方法参数名的方式小结

《Java通过反射获取方法参数名的方式小结》这篇文章主要为大家详细介绍了Java如何通过反射获取方法参数名的方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、解决方式方式2.1: 添加编译参数配置 -parameters方式2.2: 使用Spring的内部工具类 -

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo

使用Java实现获取客户端IP地址

《使用Java实现获取客户端IP地址》这篇文章主要为大家详细介绍了如何使用Java实现获取客户端IP地址,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 首先是获取 IP,直接上代码import org.springframework.web.context.request.Requ

MySQL的cpu使用率100%的问题排查流程

《MySQL的cpu使用率100%的问题排查流程》线上mysql服务器经常性出现cpu使用率100%的告警,因此本文整理一下排查该问题的常规流程,文中通过代码示例讲解的非常详细,对大家的学习或工作有一... 目录1. 确认CPU占用来源2. 实时分析mysql活动3. 分析慢查询与执行计划4. 检查索引与表

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取