UnityAppController+Thermal.mm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "UnityAppController+Thermal.h"
  2. #include "UnityAppController+Rendering.h"
  3. #include "UnityInterface.h"
  4. #import <Foundation/Foundation.h>
  5. @implementation UnityAppController (Thermal)
  6. - (void)subscribeToThermalChanges
  7. {
  8. ::printf_console("Player Setting 'Adjust iOS FPS based on thermal state' is %s\n", UnityShouldAdjustPerformanceOnThermalStateChange() ? "ON" : "OFF");
  9. [self notifyThermalState: [NSProcessInfo processInfo].thermalState];
  10. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(thermalStateDidChange:) name:NSProcessInfoThermalStateDidChangeNotification object:nil];
  11. }
  12. - (int)adjustFrameRateForThermalState:(int)targetFPS
  13. {
  14. const NSProcessInfoThermalState thermalState = [NSProcessInfo processInfo].thermalState;
  15. const int seriousFPS = UnityGetThermalStateSeriousFPS();
  16. const int criticalFPS = UnityGetThermalStateCriticalFPS();
  17. if (thermalState == NSProcessInfoThermalStateSerious && targetFPS > seriousFPS)
  18. return seriousFPS;
  19. else if (thermalState == NSProcessInfoThermalStateCritical && targetFPS > criticalFPS)
  20. return criticalFPS;
  21. return targetFPS;
  22. }
  23. - (void)thermalStateDidChange:(NSNotification*)note
  24. {
  25. [self notifyThermalState: [NSProcessInfo processInfo].thermalState];
  26. }
  27. - (void)notifyThermalState:(NSProcessInfoThermalState)state
  28. {
  29. const char* stateString = [self thermalStateToString:state];
  30. ::printf_console("thermalStateDidChange: %s\n", stateString);
  31. UnityThermalStateChanged(static_cast<int>(state));
  32. #if UNITY_USES_METAL_DISPLAY_LINK
  33. if (state == NSProcessInfoThermalStateNominal || state == NSProcessInfoThermalStateFair)
  34. {
  35. if (@available(iOS 17.0, tvOS 17.0, *))
  36. [self switchToMetalDisplayLink];
  37. }
  38. #endif
  39. if (state == NSProcessInfoThermalStateSerious || state == NSProcessInfoThermalStateCritical)
  40. [self switchToCADisplayLink];
  41. }
  42. - (const char*)thermalStateToString:(NSProcessInfoThermalState)state
  43. {
  44. switch (state)
  45. {
  46. case NSProcessInfoThermalStateNominal:
  47. return "Nominal";
  48. case NSProcessInfoThermalStateFair:
  49. return "Fair";
  50. case NSProcessInfoThermalStateSerious:
  51. return "Serious";
  52. case NSProcessInfoThermalStateCritical:
  53. return "Critical";
  54. default:
  55. return "Unknown";
  56. }
  57. }
  58. @end