UnityAppController.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #if UNITY_SUPPORT_ROTATION
  50. @property (readonly, nonatomic) UIInterfaceOrientation interfaceOrientation;
  51. #endif
  52. @property (nonatomic, retain) id renderDelegate;
  53. @property (nonatomic, copy) void(^quitHandler)();
  54. @end
  55. // accessing app controller
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. extern UnityAppController* _UnityAppController;
  60. extern UnityAppController* GetAppController();
  61. #ifdef __cplusplus
  62. } // extern "C"
  63. #endif
  64. // Put this into mm file with your subclass implementation
  65. // pass subclass name to define
  66. #define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
  67. @interface ClassName(OverrideAppDelegate) \
  68. { \
  69. } \
  70. +(void)load; \
  71. @end \
  72. @implementation ClassName(OverrideAppDelegate) \
  73. +(void)load \
  74. { \
  75. extern const char* AppControllerClassName; \
  76. AppControllerClassName = #ClassName; \
  77. } \
  78. @end \
  79. // plugins
  80. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
  81. do { \
  82. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  83. if([delegate respondsToSelector:@selector(method)]) \
  84. [delegate method]; \
  85. } while(0)
  86. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
  87. do { \
  88. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  89. if([delegate respondsToSelector:@selector(method:)]) \
  90. [delegate method:arg]; \
  91. } while(0)
  92. // these are simple wrappers about ios api, added for convenience
  93. void AppController_SendNotification(NSString* name);
  94. void AppController_SendNotificationWithArg(NSString* name, id arg);
  95. void AppController_SendUnityViewControllerNotification(NSString* name);
  96. // in the case when apple adds new api that has easy fallback path for old ios
  97. // we will add new api methods at runtime on older ios, so we can switch to new api universally
  98. // in that case we still need actual declaration: we will do it here as it is the most convenient place
  99. // history:
  100. // [CADisplayLink preferredFramesPerSecond], [UIScreen maximumFramesPerSecond], [UIView safeAreaInsets]
  101. // were removed after we started to enforce xcode9 (sdk 11)