using System; using System.ComponentModel; using System.Runtime.InteropServices; using UnityEngine; namespace UnityEngine.XR.ARFoundation.Samples { /// /// A component that provides functionality to query for thermal state on iOS devices and to subscribe to an event /// that fires when the thermal state changes. /// public class ThermalStateForIOS : MonoBehaviour { #if UNITY_IOS && !UNITY_EDITOR /// /// The thermal state on the previous update, used for determining when to fire the state changed event. /// ThermalState? m_PreviousThermalState; #endif // UNITY_IOS && !UNITY_EDITOR /// /// Event that fires when the thermal state has changed. /// public Action stateChanged; /// /// iOS thermal states as documented by /// https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/RespondToThermalStateChanges.html /// public enum ThermalState { /// /// Thermal state is unknown. /// [Description("Unknown")] Unknown = 0, /// /// The thermal state is at an acceptable level. /// [Description("Nominal")] Nominal = 1, /// /// The thermal state is minimally elevated. /// [Description("Fair")] Fair = 2, /// /// The thermal state is highly elevated. /// [Description("Serious")] Serious = 3, /// /// The thermal state is significantly elevated. /// [Description("Critical")] Critical = 4, } /// /// Queries the current thermal state of the iOS device. /// public ThermalState currentThermalState => NativeApi.GetCurrentThermalState(); #if UNITY_IOS && !UNITY_EDITOR /// /// On enable, initialize the previous thermal state to null. /// void OnEnable() { m_PreviousThermalState = null; } /// /// On each update, query the current thermal state, and fire the state changed event if the thermal state has /// changed since the previous update. /// void Update() { ThermalState thermalState = currentThermalState; if (m_PreviousThermalState.HasValue && (thermalState != m_PreviousThermalState.Value)) { if (stateChanged != null) { stateChanged(new ThermalStateChange(m_PreviousThermalState.Value, thermalState)); } } m_PreviousThermalState = thermalState; } #endif // UNITY_IOS && !UNITY_EDITOR /// /// Struct containing both the previous and current thermal states when a state change occurs. /// public struct ThermalStateChange { /// /// The previous thermal state for a state change event. /// ThermalState m_PreviousThermalState; /// /// The current thermal state for a state change event. /// ThermalState m_CurrentThermalState; /// /// The previous thermal state for a state change event. /// public ThermalState previousThermalState => m_PreviousThermalState; /// /// The current thermal state for a state change event. /// public ThermalState currentThermalState => m_CurrentThermalState; /// /// Constructs a thermal state change with the previous and current thermal states. /// /// The previous thermal state for a state change event. /// The current thermal state for a state change event. public ThermalStateChange (ThermalState previousThermalState, ThermalState currentThermalState) { m_PreviousThermalState = previousThermalState; m_CurrentThermalState = currentThermalState; } } /// /// Native API for querying the thermal state on iOS. For other platforms, this API is stubbed out. /// / static class NativeApi { #if UNITY_IOS && !UNITY_EDITOR [DllImport("__Internal", EntryPoint = "ARFoundationSamples_GetCurrentThermalState")] public static extern ThermalState GetCurrentThermalState(); #else // UNITY_IOS && !UNITY_EDITOR public static ThermalState GetCurrentThermalState() => ThermalState.Unknown; #endif // UNITY_IOS && !UNITY_EDITOR } } }