UnityAppController.h 5.2 KB

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