UnityViewControllerBase+iOS.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #if PLATFORM_IOS
  2. #import "UnityViewControllerBase.h"
  3. #import "UnityAppController.h"
  4. #include "OrientationSupport.h"
  5. #include "Keyboard.h"
  6. #include "UnityView.h"
  7. #include "PluginBase/UnityViewControllerListener.h"
  8. #include "UnityAppController.h"
  9. #include "UnityAppController+ViewHandling.h"
  10. #include "Unity/ObjCRuntime.h"
  11. // when returning from presenting UIViewController we might need to update app orientation to "correct" one, as we wont get rotation notification
  12. @interface UnityAppController ()
  13. - (void)updateAppOrientation:(UIInterfaceOrientation)orientation;
  14. @end
  15. @implementation UnityViewControllerBase (iOS)
  16. - (BOOL)shouldAutorotate
  17. {
  18. return YES;
  19. }
  20. - (BOOL)prefersStatusBarHidden
  21. {
  22. static bool _PrefersStatusBarHidden = true;
  23. static bool _PrefersStatusBarHiddenInited = false;
  24. if (!_PrefersStatusBarHiddenInited)
  25. {
  26. NSNumber* hidden = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarHidden"];
  27. _PrefersStatusBarHidden = hidden ? [hidden boolValue] : YES;
  28. _PrefersStatusBarHiddenInited = true;
  29. }
  30. return _PrefersStatusBarHidden;
  31. }
  32. - (UIStatusBarStyle)preferredStatusBarStyle
  33. {
  34. static UIStatusBarStyle _PreferredStatusBarStyle = UIStatusBarStyleDefault;
  35. static bool _PreferredStatusBarStyleInited = false;
  36. if (!_PreferredStatusBarStyleInited)
  37. {
  38. NSString* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarStyle"];
  39. if (style && [style isEqualToString: @"UIStatusBarStyleLightContent"])
  40. _PreferredStatusBarStyle = UIStatusBarStyleLightContent;
  41. _PreferredStatusBarStyleInited = true;
  42. }
  43. return _PreferredStatusBarStyle;
  44. }
  45. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
  46. {
  47. UIRectEdge res = UIRectEdgeNone;
  48. if (UnityGetDeferSystemGesturesTopEdge())
  49. res |= UIRectEdgeTop;
  50. if (UnityGetDeferSystemGesturesBottomEdge())
  51. res |= UIRectEdgeBottom;
  52. if (UnityGetDeferSystemGesturesLeftEdge())
  53. res |= UIRectEdgeLeft;
  54. if (UnityGetDeferSystemGesturesRightEdge())
  55. res |= UIRectEdgeRight;
  56. return res;
  57. }
  58. - (BOOL)prefersHomeIndicatorAutoHidden
  59. {
  60. return UnityGetHideHomeButton();
  61. }
  62. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  63. {
  64. ScreenOrientation curOrient = UIViewControllerOrientation(self);
  65. ScreenOrientation newOrient = OrientationAfterTransform(curOrient, [coordinator targetTransform]);
  66. // in case of presentation controller it will take control over orientations
  67. // so to avoid crazy corner cases, make default view controller to ignore "wrong" orientations
  68. // as they will come only in case of presentation view controller and will be reverted anyway
  69. // NB: we still want to pass message to super, we just want to skip unity-specific magic
  70. NSUInteger targetMask = 1 << ConvertToIosScreenOrientation(newOrient);
  71. if (([self supportedInterfaceOrientations] & targetMask) != 0)
  72. {
  73. [UIView setAnimationsEnabled: UnityUseAnimatedAutorotation() ? YES : NO];
  74. [KeyboardDelegate StartReorientation];
  75. [GetAppController() interfaceWillChangeOrientationTo: ConvertToIosScreenOrientation(newOrient)];
  76. [coordinator animateAlongsideTransition: nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  77. [self.view setNeedsLayout];
  78. [GetAppController() interfaceDidChangeOrientationFrom: ConvertToIosScreenOrientation(curOrient)];
  79. [KeyboardDelegate FinishReorientation];
  80. [UIView setAnimationsEnabled: YES];
  81. }];
  82. }
  83. [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
  84. }
  85. @end
  86. @implementation UnityDefaultViewController
  87. - (NSUInteger)supportedInterfaceOrientations
  88. {
  89. NSAssert(UnityShouldAutorotate(), @"UnityDefaultViewController should be used only if unity is set to autorotate");
  90. return EnabledAutorotationInterfaceOrientations();
  91. }
  92. @end
  93. @implementation UnityPortraitOnlyViewController
  94. - (NSUInteger)supportedInterfaceOrientations
  95. {
  96. return 1 << UIInterfaceOrientationPortrait;
  97. }
  98. - (void)viewWillAppear:(BOOL)animated
  99. {
  100. [GetAppController() updateAppOrientation: UIInterfaceOrientationPortrait];
  101. [super viewWillAppear: animated];
  102. }
  103. @end
  104. @implementation UnityPortraitUpsideDownOnlyViewController
  105. - (NSUInteger)supportedInterfaceOrientations
  106. {
  107. return 1 << UIInterfaceOrientationPortraitUpsideDown;
  108. }
  109. - (void)viewWillAppear:(BOOL)animated
  110. {
  111. [GetAppController() updateAppOrientation: UIInterfaceOrientationPortraitUpsideDown];
  112. [super viewWillAppear: animated];
  113. }
  114. @end
  115. @implementation UnityLandscapeLeftOnlyViewController
  116. - (NSUInteger)supportedInterfaceOrientations
  117. {
  118. return 1 << UIInterfaceOrientationLandscapeLeft;
  119. }
  120. - (void)viewWillAppear:(BOOL)animated
  121. {
  122. [GetAppController() updateAppOrientation: UIInterfaceOrientationLandscapeLeft];
  123. [super viewWillAppear: animated];
  124. }
  125. @end
  126. @implementation UnityLandscapeRightOnlyViewController
  127. - (NSUInteger)supportedInterfaceOrientations
  128. {
  129. return 1 << UIInterfaceOrientationLandscapeRight;
  130. }
  131. - (void)viewWillAppear:(BOOL)animated
  132. {
  133. [GetAppController() updateAppOrientation: UIInterfaceOrientationLandscapeRight];
  134. [super viewWillAppear: animated];
  135. }
  136. @end
  137. NSUInteger EnabledAutorotationInterfaceOrientations()
  138. {
  139. NSUInteger ret = 0;
  140. if (UnityIsOrientationEnabled(portrait))
  141. ret |= (1 << UIInterfaceOrientationPortrait);
  142. if (UnityIsOrientationEnabled(portraitUpsideDown))
  143. ret |= (1 << UIInterfaceOrientationPortraitUpsideDown);
  144. if (UnityIsOrientationEnabled(landscapeLeft))
  145. ret |= (1 << UIInterfaceOrientationLandscapeRight);
  146. if (UnityIsOrientationEnabled(landscapeRight))
  147. ret |= (1 << UIInterfaceOrientationLandscapeLeft);
  148. return ret;
  149. }
  150. #endif // PLATFORM_IOS