Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

EnergyUsage

struct in UnityEngine.AdaptivePerformance

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A collection of cross-platform per-subsystem energy consumption readings, surfaced through PerformanceMetrics.EnergyUsage.

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.

Additional resources: EnergyUsageReading, EnergyUsageSubsystem, IEnergyUsageControl, PerformanceMetrics.EnergyUsage

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.

 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

Constructor Description
EnergyUsage Initializes a new EnergyUsage with the given readings.

Public Methods

Method Description
Get Returns the reading for the given subsystem, or a default (unavailable) reading if none has been recorded.