UnityAppController.h 4.6 KB

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