Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


EnergyUsage

A collection of cross-platform per-subsystem energy consumption readings, surfaced through PerformanceMetrics.EnergyUsage.
Read time 2 minutesLast updated 7 days ago

Definition

public struct EnergyUsage

Remarks

Call EnergyUsage.Get with a EnergyUsageSubsystem value to read one subsystem's energy consumption since energy-usage tracking was started or reset with IEnergyUsageControl.StartEnergyUsageTracking.
Individual readings can be unavailable independently of each other, so check EnergyUsageReading.Available on every reading you use. Values update at the platform's native cadence, which can be considerably slower than once per frame: on Android, readings refresh roughly every 20 to 30 seconds. Poll the readings periodically rather than expecting a new value each frame.
The following example tracks energy usage while a component is enabled and logs the energy that the CPU and GPU subsystems consumed since tracking started.

Examples

using UnityEngine;using UnityEngine.AdaptivePerformance;public class EnergyUsageExample : MonoBehaviour{ IAdaptivePerformance adaptivePerformance; IEnergyUsageControl energyUsageControl; void OnEnable() { adaptivePerformance = Holder.Instance; if (adaptivePerformance == null || !adaptivePerformance.Initialized) return; energyUsageControl = adaptivePerformance.EnergyUsageControl(); if (energyUsageControl == null || !energyUsageControl.StartEnergyUsageTracking()) Debug.Log("Energy-usage tracking isn't available on this device."); } void Update() { if (energyUsageControl == null || !energyUsageControl.EnergyUsageTrackingActive) return; EnergyUsage energyUsage = adaptivePerformance.PerformanceStatus.PerformanceMetrics.EnergyUsage; LogSubsystem(energyUsage, EnergyUsageSubsystem.Cpu); LogSubsystem(energyUsage, EnergyUsageSubsystem.Gpu); } void LogSubsystem(EnergyUsage energyUsage, EnergyUsageSubsystem subsystem) { EnergyUsageReading reading = energyUsage.Get(subsystem); // This device doesn't report energy for this subsystem, so skip it. if (!reading.Available) return; Debug.Log($"{subsystem} used {reading.Energy} microwatt-seconds over {reading.Interval} ms."); } void OnDisable() { energyUsageControl?.StopEnergyUsageTracking(); }}

Constructors

Methods

Method

Description

GetReturns the reading for the given subsystem, or a default (unavailable) reading if none has been recorded.