UnityAppController.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #import <QuartzCore/CADisplayLink.h>
  3. // we want to use CAMetalDisplayLink if it is available
  4. // if CADisplayLink is always preferred, this can be changed to #define UNITY_USES_METAL_DISPLAY_LINK 0
  5. // further, shouldUseMetalDisplayLink method can be overriden/tweaked to disable metal display link at runtime
  6. // e.g. you might want to use old display link on slower devices where stable frametime cannot be guaranteed
  7. #define UNITY_USES_METAL_DISPLAY_LINK (UNITY_HAS_IOSSDK_17_0 || UNITY_HAS_TVOSSDK_17_0)
  8. #if UNITY_USES_METAL_DISPLAY_LINK
  9. #import <QuartzCore/CAMetalDisplayLink.h>
  10. #endif
  11. #import <UnityFramework/RenderPluginDelegate.h>
  12. @class UnityView;
  13. @class UnityViewControllerBase;
  14. @class DisplayConnection;
  15. typedef enum
  16. {
  17. kUnityEngineLoadStateNotStarted = 0,
  18. // Minimal initialization done, allowing limited API use, such as reporting URL app was launched with
  19. kUnityEngineLoadStateMinimal = 1,
  20. // Core of Unity engine is loaded, but no graphics or first scene yet
  21. kUnityEngineLoadStateCoreInitialized = 2,
  22. // Rendering was initialized, nothing related to rendering should be touched before this state
  23. kUnityEngineLoadStateRenderingInitialized = 3,
  24. // Unity is fully initialized, it's not safe to call Unity APIs before this state
  25. kUnityEngineLoadStateAppReady = 4,
  26. } UnityEngineLoadState;
  27. __attribute__ ((visibility("default")))
  28. @interface UnityAppController : NSObject<UIApplicationDelegate>
  29. {
  30. UnityView* _unityView;
  31. CADisplayLink* _displayLink;
  32. #if UNITY_USES_METAL_DISPLAY_LINK
  33. CAMetalDisplayLink* _metalDisplayLink API_AVAILABLE(ios(17.0), tvos(17.0));
  34. #endif
  35. // since we can have CAMetalDisplayLink not available both at runtime (old ios) and at compile time (old xcode)
  36. // and we can have it disabled completely (not all applications need it)
  37. // the code supporting both CAMetalDisplayLink and CADisplayLink can easily become very convoluted
  38. // hence we add a boolean to check CAMetalDisplayLink usage without checks for @available and preprocessor ifdefs
  39. BOOL _usesMetalDisplayLink;
  40. UIWindow* _window;
  41. UIView* _rootView;
  42. UIViewController* _rootController;
  43. UIViewController* _snapshotViewController;
  44. DisplayConnection* _mainDisplay;
  45. // CODE ARCHEOLOGY: we were caching view controllers, both autorotation one and per-fixed-orientation ones
  46. // CODE ARCHEOLOGY: we stopped doing this as the performance impact is negligible,
  47. // CODE ARCHEOLOGY: yet it introduces corner cases and in general lots of code
  48. #if UNITY_SUPPORT_ROTATION
  49. UIInterfaceOrientation _curOrientation;
  50. #endif
  51. id<RenderPluginDelegate> _renderDelegate;
  52. }
  53. // override it to add your render plugin delegate
  54. - (void)shouldAttachRenderDelegate;
  55. // this one is called at the very end of didFinishLaunchingWithOptions:
  56. // after views have been created but before initing engine itself
  57. // override it to register plugins, tweak UI etc
  58. - (void)preStartUnity;
  59. - (void)initUnityApplicationNoGraphics;
  60. // this one is called at at the very end of didFinishLaunchingWithOptions:
  61. // it will start showing unity view and rendering unity content
  62. - (void)initUnityWithScene:(UIWindowScene*)scene;
  63. // this one is called at at the very end of didFinishLaunchingWithOptions:
  64. // it will start showing unity view and rendering unity content
  65. - (void)startUnity;
  66. // override it if you want to have custom logic for the decision to use CAMetalDisplayLink or not
  67. // in any case, CAMetalDisplayLink will be used only if actually supported by the device
  68. // this will be called once on startup, before any rendering but after initializing unity
  69. - (BOOL)shouldUseMetalDisplayLink;
  70. - (BOOL)advanceEngineLoadState:(UnityEngineLoadState)newState;
  71. - (BOOL)downgradeEngineLoadState:(UnityEngineLoadState)newState;
  72. // this is a part of UIApplicationDelegate protocol starting with ios5
  73. // setter will be generated empty
  74. @property (retain, nonatomic) UIWindow* window;
  75. @property (readonly, copy, nonatomic) UnityView* unityView;
  76. @property (readonly, copy, nonatomic) CADisplayLink* unityDisplayLink;
  77. @property (readonly, nonatomic) BOOL unityUsesMetalDisplayLink;
  78. #if UNITY_USES_METAL_DISPLAY_LINK
  79. @property (readonly, copy, nonatomic) CAMetalDisplayLink* unityMetalDisplayLink API_AVAILABLE(ios(17.0), tvos(17.0));
  80. #endif
  81. @property (readonly, copy, nonatomic) UIView* rootView;
  82. @property (readonly, copy, nonatomic) UIViewController* rootViewController;
  83. @property (readonly, copy, nonatomic) DisplayConnection* mainDisplay;
  84. #if UNITY_SUPPORT_ROTATION
  85. @property (readonly, nonatomic) UIInterfaceOrientation interfaceOrientation;
  86. #endif
  87. @property (readonly) UnityEngineLoadState engineLoadState;
  88. @property (readonly) bool didResignActive;
  89. @property (nonatomic, retain) id renderDelegate;
  90. @property (nonatomic, copy) void (^quitHandler)(void);
  91. @end
  92. // accessing app controller
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96. extern UnityAppController* _UnityAppController;
  97. extern UnityAppController* GetAppController(void);
  98. #ifdef __cplusplus
  99. } // extern "C"
  100. #endif
  101. // Put this into mm file with your subclass implementation
  102. // pass subclass name to define
  103. #define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
  104. @interface ClassName(OverrideAppDelegate) \
  105. { \
  106. } \
  107. +(void)load; \
  108. @end \
  109. @implementation ClassName(OverrideAppDelegate) \
  110. +(void)load \
  111. { \
  112. extern const char* AppControllerClassName; \
  113. AppControllerClassName = #ClassName; \
  114. } \
  115. @end \
  116. // plugins
  117. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
  118. do { \
  119. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  120. if([delegate respondsToSelector:@selector(method)]) \
  121. [delegate method]; \
  122. } while(0)
  123. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
  124. do { \
  125. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  126. if([delegate respondsToSelector:@selector(method:)]) \
  127. [delegate method:arg]; \
  128. } while(0)
  129. // these are simple wrappers about ios api, added for convenience
  130. void AppController_SendNotification(NSString* name);
  131. void AppController_SendNotificationWithArg(NSString* name, id arg);
  132. void AppController_SendUnityViewControllerNotification(NSString* name);
  133. // in the case when apple adds new api that has easy fallback path for old ios
  134. // we will add new api methods at runtime on older ios, so we can switch to new api universally
  135. // in that case we still need actual declaration: we will do it here as it is the most convenient place
  136. // history:
  137. // [CADisplayLink preferredFramesPerSecond], [UIScreen maximumFramesPerSecond], [UIView safeAreaInsets]
  138. // were removed after we started to enforce xcode9 (sdk 11)