UnityAppController.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #import <QuartzCore/CADisplayLink.h>
  3. #include "RenderPluginDelegate.h"
  4. #include <CompositorServices/CompositorServices.h>
  5. @class UnityView;
  6. @class UnityViewControllerBase;
  7. @class DisplayConnection;
  8. __attribute__ ((visibility("default")))
  9. @interface UnityAppController : NSObject<UIApplicationDelegate>
  10. {
  11. UnityView* _unityView;
  12. CADisplayLink* _displayLink;
  13. UIWindow* _window;
  14. UIView* _rootView;
  15. UIViewController* _rootController;
  16. UIViewController* _snapshotViewController;
  17. DisplayConnection* _mainDisplay;
  18. // CODE ARCHEOLOGY: we were caching view controllers, both autorotation one and per-fixed-orientation ones
  19. // CODE ARCHEOLOGY: we stopped doing this as the performance impact is negligible,
  20. // CODE ARCHEOLOGY: yet it introduces corner cases and in general lots of code
  21. #if UNITY_SUPPORT_ROTATION
  22. UIInterfaceOrientation _curOrientation;
  23. #endif
  24. id<RenderPluginDelegate> _renderDelegate;
  25. }
  26. // override it to add your render plugin delegate
  27. - (void)shouldAttachRenderDelegate;
  28. // this one is called at the very end of didFinishLaunchingWithOptions:
  29. // after views have been created but before initing engine itself
  30. // override it to register plugins, tweak UI etc
  31. - (void)preStartUnity;
  32. // this one is called at at the very end of didFinishLaunchingWithOptions:
  33. // it will start showing unity view and rendering unity content
  34. - (void)startUnity:(UIApplication*)application;
  35. // this is a part of UIApplicationDelegate protocol starting with ios5
  36. // setter will be generated empty
  37. @property (retain, nonatomic) UIWindow* window;
  38. @property (readonly, copy, nonatomic) UnityView* unityView;
  39. @property (readonly, copy, nonatomic) CADisplayLink* unityDisplayLink;
  40. @property (readonly, copy, nonatomic) UIView* rootView;
  41. @property (readonly, copy, nonatomic) UIViewController* rootViewController;
  42. @property (readonly, copy, nonatomic) DisplayConnection* mainDisplay;
  43. #if UNITY_SUPPORT_ROTATION
  44. @property (readonly, nonatomic) UIInterfaceOrientation interfaceOrientation;
  45. #endif
  46. @property (nonatomic, retain) id renderDelegate;
  47. @property (nonatomic, copy) void (^quitHandler)(void);
  48. @end
  49. // accessing app controller
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. extern UnityAppController* _UnityAppController;
  54. extern UnityAppController* GetAppController(void);
  55. #ifdef __cplusplus
  56. } // extern "C"
  57. #endif
  58. // Put this into mm file with your subclass implementation
  59. // pass subclass name to define
  60. #define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
  61. @interface ClassName(OverrideAppDelegate) \
  62. { \
  63. } \
  64. +(void)load; \
  65. @end \
  66. @implementation ClassName(OverrideAppDelegate) \
  67. +(void)load \
  68. { \
  69. extern const char* AppControllerClassName; \
  70. AppControllerClassName = #ClassName; \
  71. } \
  72. @end \
  73. // plugins
  74. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD(method) \
  75. do { \
  76. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  77. if([delegate respondsToSelector:@selector(method)]) \
  78. [delegate method]; \
  79. } while(0)
  80. #define APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(method, arg) \
  81. do { \
  82. id<RenderPluginDelegate> delegate = GetAppController().renderDelegate; \
  83. if([delegate respondsToSelector:@selector(method:)]) \
  84. [delegate method:arg]; \
  85. } while(0)
  86. // these are simple wrappers about ios api, added for convenience
  87. void AppController_SendNotification(NSString* name);
  88. void AppController_SendNotificationWithArg(NSString* name, id arg);
  89. void AppController_SendUnityViewControllerNotification(NSString* name);
  90. // in the case when apple adds new api that has easy fallback path for old ios
  91. // we will add new api methods at runtime on older ios, so we can switch to new api universally
  92. // in that case we still need actual declaration: we will do it here as it is the most convenient place
  93. // history:
  94. // [CADisplayLink preferredFramesPerSecond], [UIScreen maximumFramesPerSecond], [UIView safeAreaInsets]
  95. // were removed after we started to enforce xcode9 (sdk 11)
  96. // LayerRenderer is used in both UnityAppController and UnityAppController+Rendering
  97. extern cp_layer_renderer_t _LayerRenderer;