UnityAppController.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. @end
  62. // accessing app controller
  63. extern UnityAppController* _UnityAppController;
  64. inline UnityAppController* GetAppController()
  65. {
  66. return _UnityAppController;
  67. }
  68. // Put this into mm file with your subclass implementation
  69. // pass subclass name to define
  70. #define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
  71. @interface ClassName(OverrideAppDelegate) \
  72. { \
  73. } \
  74. +(void)load; \
  75. @end \
  76. @implementation ClassName(OverrideAppDelegate) \
  77. +(void)load \
  78. { \
  79. extern const char* AppControllerClassName; \
  80. AppControllerClassName = #ClassName; \
  81. } \
  82. @end \
  83. // plugins
  84. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
  85. do { \
  86. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  87. if([delegate respondsToSelector:@selector(method)]) \
  88. [delegate method]; \
  89. } while(0)
  90. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
  91. do { \
  92. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  93. if([delegate respondsToSelector:@selector(method:)]) \
  94. [delegate method:arg]; \
  95. } while(0)
  96. // these are simple wrappers about ios api, added for convenience
  97. void AppController_SendNotification(NSString* name);
  98. void AppController_SendNotificationWithArg(NSString* name, id arg);
  99. void AppController_SendUnityViewControllerNotification(NSString* name);
  100. // in the case when apple adds new api that has easy fallback path for old ios
  101. // we will add new api methods at runtime on older ios, so we can switch to new api universally
  102. // in that case we still need actual declaration: we do it here as most convenient place
  103. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_10_0) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_10_0)
  104. @interface CADisplayLink ()
  105. @property(nonatomic) NSInteger preferredFramesPerSecond;
  106. @end
  107. #endif
  108. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_10_3) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_10_2)
  109. // The maximumFramesPerSecond API is available in the SDKs since 10.3.
  110. // However, tvOS SDK has it already in 10.2, but disabled.
  111. @interface UIScreen ()
  112. @property (readonly) NSInteger maximumFramesPerSecond;
  113. @end
  114. #endif
  115. #if (PLATFORM_IOS && !UNITY_HAS_IOSSDK_11_0) || (PLATFORM_TVOS && !UNITY_HAS_TVOSSDK_11_0)
  116. // The safeAreaInsets API is available in the SDKs since 11.0.
  117. @interface UIView ()
  118. @property (nonatomic, readonly) UIEdgeInsets safeAreaInsets;
  119. @end
  120. #endif