UnityAppController.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #import <QuartzCore/CADisplayLink.h>
  3. #include "PluginBase/RenderPluginDelegate.h"
  4. @class UnityView;
  5. @class UnityViewControllerBase;
  6. @class DisplayConnection;
  7. @interface UnityAppController : NSObject<UIApplicationDelegate>
  8. {
  9. UnityView* _unityView;
  10. CADisplayLink* _displayLink;
  11. UIWindow* _window;
  12. UIView* _rootView;
  13. UIViewController* _rootController;
  14. UIView* _snapshotView;
  15. DisplayConnection* _mainDisplay;
  16. // We will cache view controllers used for fixed orientation (indexed by UIInterfaceOrientation).
  17. // Default view contoller goes to index 0. The default view controller is
  18. // used when autorotation is enabled.
  19. //
  20. // There's no way to force iOS to change orientation when autorotation is enabled and
  21. // the current orientation is disabled. [UIViewController attemptRotationToDeviceOrientation]
  22. // is insufficient to force iOS to change orientation in this circumstance.
  23. //
  24. // To work around this there's an additional view controller. We switch to it when the
  25. // autorotating view controller is used and we detect that the current orientation has been
  26. // disabled. The controller is swapped with _viewControllerForOrientation[0] immediately,
  27. // so _secondaryAutorotatingViewController is never the actual active controller and can be
  28. // ignored for most purposes.
  29. #if UNITY_SUPPORT_ROTATION
  30. UIViewController* _viewControllerForOrientation[5];
  31. UIInterfaceOrientation _curOrientation;
  32. UIViewController* _secondaryAutorotatingViewController;
  33. #else
  34. UIViewController* _viewControllerForOrientation[1];
  35. #endif
  36. id<RenderPluginDelegate> _renderDelegate;
  37. }
  38. // override it to add your render plugin delegate
  39. - (void)shouldAttachRenderDelegate;
  40. // this one is called at the very end of didFinishLaunchingWithOptions:
  41. // after views have been created but before initing engine itself
  42. // override it to register plugins, tweak UI etc
  43. - (void)preStartUnity;
  44. // this one is called at first applicationDidBecomeActive
  45. // NB: it will be started with delay 0, so it will run on next run loop iteration
  46. // this is done to make sure that activity indicator animation starts before blocking loading
  47. - (void)startUnity:(UIApplication*)application;
  48. // this is a part of UIApplicationDelegate protocol starting with ios5
  49. // setter will be generated empty
  50. @property (retain, nonatomic) UIWindow* window;
  51. @property (readonly, copy, nonatomic) UnityView* unityView;
  52. @property (readonly, copy, nonatomic) CADisplayLink* unityDisplayLink;
  53. @property (readonly, copy, nonatomic) UIView* rootView;
  54. @property (readonly, copy, nonatomic) UIViewController* rootViewController;
  55. @property (readonly, copy, nonatomic) DisplayConnection* mainDisplay;
  56. #if UNITY_SUPPORT_ROTATION
  57. @property (readonly, nonatomic) UIInterfaceOrientation interfaceOrientation;
  58. #endif
  59. @property (nonatomic, retain) id renderDelegate;
  60. @property (nonatomic, copy) void(^quitHandler)();
  61. //leon add保活
  62. @property (nonatomic,assign) UIBackgroundTaskIdentifier bgTask;
  63. @end
  64. // accessing app controller
  65. extern UnityAppController* _UnityAppController;
  66. inline UnityAppController* GetAppController()
  67. {
  68. return _UnityAppController;
  69. }
  70. // Put this into mm file with your subclass implementation
  71. // pass subclass name to define
  72. #define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
  73. @interface ClassName(OverrideAppDelegate) \
  74. { \
  75. } \
  76. +(void)load; \
  77. @end \
  78. @implementation ClassName(OverrideAppDelegate) \
  79. +(void)load \
  80. { \
  81. extern const char* AppControllerClassName; \
  82. AppControllerClassName = #ClassName; \
  83. } \
  84. @end \
  85. // plugins
  86. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
  87. do { \
  88. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  89. if([delegate respondsToSelector:@selector(method)]) \
  90. [delegate method]; \
  91. } while(0)
  92. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
  93. do { \
  94. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  95. if([delegate respondsToSelector:@selector(method:)]) \
  96. [delegate method:arg]; \
  97. } while(0)
  98. // these are simple wrappers about ios api, added for convenience
  99. void AppController_SendNotification(NSString* name);
  100. void AppController_SendNotificationWithArg(NSString* name, id arg);
  101. void AppController_SendUnityViewControllerNotification(NSString* name);
  102. // in the case when apple adds new api that has easy fallback path for old ios
  103. // we will add new api methods at runtime on older ios, so we can switch to new api universally
  104. // in that case we still need actual declaration: we do it here as most convenient place
  105. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_10_0) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_10_0)
  106. @interface CADisplayLink ()
  107. @property(nonatomic) NSInteger preferredFramesPerSecond;
  108. @end
  109. #endif
  110. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_10_3) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_10_2)
  111. // The maximumFramesPerSecond API is available in the SDKs since 10.3.
  112. // However, tvOS SDK has it already in 10.2, but disabled.
  113. @interface UIScreen ()
  114. @property (readonly) NSInteger maximumFramesPerSecond;
  115. @end
  116. #endif
  117. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_11_0) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_11_0)
  118. // The safeAreaInsets API is available in the SDKs since 11.0.
  119. @interface UIView ()
  120. @property (nonatomic, readonly) UIEdgeInsets safeAreaInsets;
  121. @end
  122. #endif