UnityViewControllerBase+iOS.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 orientation changes with animation, pause rendering for this long so resolution change happens during animation with less visual distortion
  12. const NSTimeInterval REORIENTATION_RENDERING_PAUSE = 0.15;
  13. // when returning from presenting UIViewController we might need to update app orientation to "correct" one, as we wont get rotation notification
  14. @interface UnityAppController ()
  15. - (void)updateAppOrientation:(UIInterfaceOrientation)orientation;
  16. @end
  17. #ifndef __IPHONE_16_0
  18. @interface UIViewController ()
  19. - (void)setNeedsUpdateOfSupportedInterfaceOrientations;
  20. @end
  21. #endif
  22. @implementation UnityViewControllerBase (iOS)
  23. - (BOOL)shouldAutorotate
  24. {
  25. return YES;
  26. }
  27. - (BOOL)prefersStatusBarHidden
  28. {
  29. static bool _PrefersStatusBarHidden = true;
  30. static bool _PrefersStatusBarHiddenInited = false;
  31. if (!_PrefersStatusBarHiddenInited)
  32. {
  33. NSNumber* hidden = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarHidden"];
  34. _PrefersStatusBarHidden = hidden ? [hidden boolValue] : YES;
  35. _PrefersStatusBarHiddenInited = true;
  36. }
  37. return _PrefersStatusBarHidden;
  38. }
  39. - (UIStatusBarStyle)preferredStatusBarStyle
  40. {
  41. static UIStatusBarStyle _PreferredStatusBarStyle = UIStatusBarStyleDefault;
  42. static bool _PreferredStatusBarStyleInited = false;
  43. if (!_PreferredStatusBarStyleInited)
  44. {
  45. NSString* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarStyle"];
  46. if (style && [style isEqualToString: @"UIStatusBarStyleLightContent"])
  47. _PreferredStatusBarStyle = UIStatusBarStyleLightContent;
  48. if (style && [style isEqualToString: @"UIStatusBarStyleDarkContent"])
  49. _PreferredStatusBarStyle = UIStatusBarStyleDarkContent;
  50. _PreferredStatusBarStyleInited = true;
  51. }
  52. return _PreferredStatusBarStyle;
  53. }
  54. - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
  55. {
  56. UIRectEdge res = UIRectEdgeNone;
  57. if (UnityGetDeferSystemGesturesTopEdge())
  58. res |= UIRectEdgeTop;
  59. if (UnityGetDeferSystemGesturesBottomEdge())
  60. res |= UIRectEdgeBottom;
  61. if (UnityGetDeferSystemGesturesLeftEdge())
  62. res |= UIRectEdgeLeft;
  63. if (UnityGetDeferSystemGesturesRightEdge())
  64. res |= UIRectEdgeRight;
  65. return res;
  66. }
  67. - (BOOL)prefersHomeIndicatorAutoHidden
  68. {
  69. return UnityGetHideHomeButton();
  70. }
  71. @end
  72. @implementation UnityDefaultViewController
  73. {
  74. // these will be updated in one place where we "sync" UI side orientation handling to unity side
  75. NSUInteger _supportedOrientations;
  76. // this will be updated in one place where we "sync" UI side orientation handling to unity side
  77. UIInterfaceOrientation _fixedOrientation;
  78. // this indicates if we are asked to handle fixed orientation too - app should decide
  79. BOOL _willHandleFixedOrientation;
  80. }
  81. - (BOOL)willHandleFixedOrientation
  82. {
  83. return _willHandleFixedOrientation;
  84. }
  85. - (void)readOrientationFromUnity
  86. {
  87. if(UnityShouldAutorotate())
  88. {
  89. _fixedOrientation = UIInterfaceOrientationUnknown;
  90. _supportedOrientations = EnabledAutorotationInterfaceOrientations();
  91. }
  92. else
  93. {
  94. _fixedOrientation = ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation());
  95. _supportedOrientations = (1 << _fixedOrientation);
  96. }
  97. }
  98. - (instancetype)initShouldHandleFixedOrientation:(BOOL)shouldHandleFixedOrientation
  99. {
  100. if ((self = [super init]))
  101. {
  102. _willHandleFixedOrientation = shouldHandleFixedOrientation;
  103. NSAssert(UnityShouldAutorotate() || _willHandleFixedOrientation,
  104. @"UnityDefaultViewController should be used either if unity is set to autorotate, or if asked explicitly to handle fixed orientation");
  105. [self readOrientationFromUnity];
  106. }
  107. return self;
  108. }
  109. - (void)updateSupportedOrientations
  110. {
  111. [self readOrientationFromUnity];
  112. if (@available(iOS 16.0, *))
  113. [self setNeedsUpdateOfSupportedInterfaceOrientations];
  114. }
  115. - (NSUInteger)supportedInterfaceOrientations
  116. {
  117. return _supportedOrientations;
  118. }
  119. - (void)viewWillAppear:(BOOL)animated
  120. {
  121. ScreenOrientation currentOrientation = UIViewControllerOrientation(self);
  122. [GetAppController() updateAppOrientation: ConvertToIosScreenOrientation(currentOrientation)];
  123. [super viewWillAppear: animated];
  124. }
  125. - (void)viewDidAppear:(BOOL)animated
  126. {
  127. ScreenOrientation currentOrientation = UIViewControllerOrientation(self);
  128. [GetAppController() updateAppOrientation: ConvertToIosScreenOrientation(currentOrientation)];
  129. [super viewDidAppear: animated];
  130. }
  131. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  132. {
  133. // CODE ARCHEOLOGY: we were using UIViewControllerOrientation, but on showing view with "Requires full screen"
  134. // CODE ARCHEOLOGY: we will get the size/orientation *already* set, and the rotation logic would break
  135. const ScreenOrientation curOrient = GetAppController().unityView.contentOrientation;
  136. const ScreenOrientation newOrient = OrientationAfterTransform(curOrient, [coordinator targetTransform]);
  137. // delay resolution change, ideally we want it to happen in the middle of rotation animation
  138. // we force rendering back upon completion, just in case transition happens sooner
  139. // NOTE: with CAMetalDisplayLink we cannot just skip rendering, hence the if
  140. if(!GetAppController().unityUsesMetalDisplayLink)
  141. {
  142. GetAppController().unityView.skipRendering = YES;
  143. [GetAppController().unityView performSelector: @selector(resumeRendering) withObject: nil afterDelay: REORIENTATION_RENDERING_PAUSE];
  144. }
  145. // in case of presentation controller it will take control over orientations
  146. // so to avoid crazy corner cases, make default view controller to ignore "wrong" orientations
  147. // as they will come only in case of presentation view controller and will be reverted anyway
  148. // NB: we still want to pass message to super, we just want to skip unity-specific magic
  149. NSUInteger targetMask = 1 << ConvertToIosScreenOrientation(newOrient);
  150. if (([self supportedInterfaceOrientations] & targetMask) != 0)
  151. {
  152. [UIView setAnimationsEnabled: UnityUseAnimatedAutorotation() ? YES : NO];
  153. [KeyboardDelegate StartReorientation];
  154. [GetAppController() interfaceWillChangeOrientationTo: ConvertToIosScreenOrientation(newOrient)];
  155. [coordinator animateAlongsideTransition: nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  156. [self.view setNeedsLayout];
  157. [GetAppController() interfaceDidChangeOrientationFrom: ConvertToIosScreenOrientation(curOrient)];
  158. [KeyboardDelegate FinishReorientation];
  159. [UIView setAnimationsEnabled: YES];
  160. GetAppController().unityView.skipRendering = NO;
  161. }];
  162. }
  163. [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
  164. }
  165. @end
  166. @interface UnityFixedOrientationViewController()
  167. {
  168. UIInterfaceOrientation _fixedOrientation;
  169. }
  170. @end
  171. @implementation UnityFixedOrientationViewController
  172. - (instancetype)initWithOrientation:(UIInterfaceOrientation)interfaceOrientation
  173. {
  174. self = [super init];
  175. if (self)
  176. {
  177. _fixedOrientation = interfaceOrientation;
  178. }
  179. return self;
  180. }
  181. - (NSUInteger)supportedInterfaceOrientations
  182. {
  183. return 1 << _fixedOrientation;
  184. }
  185. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  186. {
  187. return _fixedOrientation;
  188. }
  189. - (void)viewWillAppear:(BOOL)animated
  190. {
  191. [GetAppController() updateAppOrientation: _fixedOrientation];
  192. [super viewWillAppear: animated];
  193. }
  194. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  195. {
  196. // Adding a call to notify about changed orientation. In iOS16 there was a runtime change where view controller
  197. // is not fully set up as soon as we make the view controller change in AppController at -transitionToViewController:.
  198. // And instead iOS calls this method, in other cases this method is not called. So we want to call
  199. // didTransitionToViewController here as this is the place where we get updated orientation.
  200. //
  201. // NB: Look for additional explanation at UnityAppController+ViewHandling.mm method -transitionToViewController: before
  202. // call to same method.
  203. [GetAppController() didTransitionToViewController: self fromViewController: self];
  204. [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
  205. }
  206. @end
  207. @implementation UnityPortraitOnlyViewController
  208. - (instancetype)init
  209. {
  210. self = [super initWithOrientation: UIInterfaceOrientationPortrait];
  211. return self;
  212. }
  213. @end
  214. @implementation UnityPortraitUpsideDownOnlyViewController
  215. - (instancetype)init
  216. {
  217. self = [super initWithOrientation: UIInterfaceOrientationPortraitUpsideDown];
  218. return self;
  219. }
  220. @end
  221. @implementation UnityLandscapeLeftOnlyViewController
  222. - (instancetype)init
  223. {
  224. self = [super initWithOrientation: UIInterfaceOrientationLandscapeLeft];
  225. return self;
  226. }
  227. @end
  228. @implementation UnityLandscapeRightOnlyViewController
  229. - (instancetype)init
  230. {
  231. self = [super initWithOrientation: UIInterfaceOrientationLandscapeRight];
  232. return self;
  233. }
  234. @end
  235. NSUInteger EnabledAutorotationInterfaceOrientations()
  236. {
  237. NSUInteger ret = 0;
  238. if (UnityIsOrientationEnabled(portrait))
  239. ret |= (1 << UIInterfaceOrientationPortrait);
  240. if (UnityDeviceSupportsUpsideDown() && UnityIsOrientationEnabled(portraitUpsideDown))
  241. ret |= (1 << UIInterfaceOrientationPortraitUpsideDown);
  242. if (UnityIsOrientationEnabled(landscapeLeft))
  243. ret |= (1 << UIInterfaceOrientationLandscapeRight);
  244. if (UnityIsOrientationEnabled(landscapeRight))
  245. ret |= (1 << UIInterfaceOrientationLandscapeLeft);
  246. // Handling unexpected case where autorotation is on and all the orientations are off by defaulting to current orientation.
  247. // Previously we returned 0 and iOS were handling it by keeping orientation as is. From iOS16 behaviour changed and the bug was raised.
  248. // Either way iOS requires us to provide non 0 value to supportedInterfaceOrientations.
  249. if (ret == 0)
  250. {
  251. NSLog(@"[Error] All orientations are off for autorotation. Preventing crash by using current orientation.");
  252. ret = (1 << [GetAppController() interfaceOrientation]);
  253. }
  254. return ret;
  255. }
  256. #endif // PLATFORM_IOS