UnityAppController+ViewHandling.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "UnityAppController+ViewHandling.h"
  2. #include "UnityAppController+Rendering.h"
  3. #include "UI/OrientationSupport.h"
  4. #include "UI/UnityView.h"
  5. #include "UI/UnityViewControllerBase.h"
  6. #include "Unity/DisplayManager.h"
  7. // TEMP: ?
  8. #include "UI/ActivityIndicator.h"
  9. #include "UI/SplashScreen.h"
  10. #include "UI/Keyboard.h"
  11. #include <utility>
  12. extern bool _skipPresent;
  13. extern bool _unityAppReady;
  14. @implementation UnityAppController (ViewHandling)
  15. #if UNITY_SUPPORT_ROTATION
  16. // special case for when we DO know the app orientation, but dont get it through normal mechanism (UIViewController orientation handling)
  17. // how can this happen:
  18. // 1. On startup: ios is not sending "change orientation" notifications on startup (but rather we "start" in correct one already)
  19. // 2. When using presentation controller it can override orientation constraints, so on dismissing we need to tweak app orientation;
  20. // pretty much like startup situation UIViewController would have correct orientation, and app will be out-of-sync
  21. - (void)updateAppOrientation:(UIInterfaceOrientation)orientation
  22. {
  23. _curOrientation = orientation;
  24. [_unityView willRotateToOrientation: orientation fromOrientation: (UIInterfaceOrientation)UIInterfaceOrientationUnknown];
  25. [_unityView didRotate];
  26. }
  27. #endif
  28. - (UnityView*)createUnityView
  29. {
  30. return [[UnityView alloc] initFromMainScreen];
  31. }
  32. - (UIViewController*)createUnityViewControllerDefault
  33. {
  34. UnityDefaultViewController* ret = [[UnityDefaultViewController alloc] init];
  35. #if PLATFORM_TVOS
  36. // This enables game controller use in on-screen keyboard
  37. ret.controllerUserInteractionEnabled = YES;
  38. #endif
  39. return ret;
  40. }
  41. #if UNITY_SUPPORT_ROTATION
  42. - (UIViewController*)createUnityViewControllerForOrientation:(UIInterfaceOrientation)orient
  43. {
  44. switch (orient)
  45. {
  46. case UIInterfaceOrientationPortrait: return [[UnityPortraitOnlyViewController alloc] init];
  47. case UIInterfaceOrientationPortraitUpsideDown: return [[UnityPortraitUpsideDownOnlyViewController alloc] init];
  48. case UIInterfaceOrientationLandscapeLeft: return [[UnityLandscapeLeftOnlyViewController alloc] init];
  49. case UIInterfaceOrientationLandscapeRight: return [[UnityLandscapeRightOnlyViewController alloc] init];
  50. default: NSAssert(false, @"bad UIInterfaceOrientation provided");
  51. }
  52. return nil;
  53. }
  54. #endif
  55. - (UIViewController*)createRootViewController
  56. {
  57. UIViewController* ret = nil;
  58. if (!UNITY_SUPPORT_ROTATION || UnityShouldAutorotate())
  59. {
  60. if (_viewControllerForOrientation[0] == nil)
  61. _viewControllerForOrientation[0] = [self createUnityViewControllerDefault];
  62. ret = _viewControllerForOrientation[0];
  63. }
  64. #if UNITY_SUPPORT_ROTATION
  65. if (ret == nil)
  66. {
  67. UIInterfaceOrientation orientation = ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation());
  68. ret = [self createRootViewControllerForOrientation: orientation];
  69. }
  70. #endif
  71. return ret;
  72. }
  73. - (UIViewController*)topMostController
  74. {
  75. UIViewController *topController = self.window.rootViewController;
  76. while (topController.presentedViewController)
  77. topController = topController.presentedViewController;
  78. return topController;
  79. }
  80. - (void)willStartWithViewController:(UIViewController*)controller
  81. {
  82. _unityView.contentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  83. _unityView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  84. _rootController.view = _rootView = _unityView;
  85. }
  86. - (void)willTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  87. {
  88. }
  89. - (void)didTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  90. {
  91. #if UNITY_SUPPORT_ROTATION
  92. // when transitioning between view controllers ios will not send reorient events (because they are bound to controllers, not view)
  93. // so we imitate them here so unity view can update its size/orientation
  94. [_unityView willRotateToOrientation: UIViewControllerInterfaceOrientation(toController) fromOrientation: ConvertToIosScreenOrientation(_unityView.contentOrientation)];
  95. [_unityView didRotate];
  96. // NB: this is both important and insane at the same time (that we have several places to keep current orentation and we need to sync them)
  97. _curOrientation = UIViewControllerInterfaceOrientation(toController);
  98. #endif
  99. }
  100. - (UIView*)createSnapshotView
  101. {
  102. // Snapshot API appeared on iOS 7, however before iOS 8 tweaking hierarchy like that on going to
  103. // background results in all kind of weird things when going back to foreground so we do snapshotting
  104. // only on iOS 8 and newer.
  105. // Note that on iPads with iOS 9 or later (up to iOS 10.2 at least) there's a bug in the iOS
  106. // compositor: any use of -[UIView snapshotViewAfterScreenUpdates] causes black screen being shown
  107. // temporarily when 4 finger gesture to swipe to another app in the task switcher is being performed slowly
  108. #if UNITY_SNAPSHOT_VIEW_ON_APPLICATION_PAUSE
  109. return [_rootView snapshotViewAfterScreenUpdates: YES];
  110. #else
  111. return nil;
  112. #endif
  113. }
  114. - (void)createUI
  115. {
  116. NSAssert(_unityView != nil, @"_unityView should be inited at this point");
  117. NSAssert(_window != nil, @"_window should be inited at this point");
  118. _rootController = [self createRootViewController];
  119. [self willStartWithViewController: _rootController];
  120. NSAssert(_rootView != nil, @"_rootView should be inited at this point");
  121. NSAssert(_rootController != nil, @"_rootController should be inited at this point");
  122. [_window makeKeyAndVisible];
  123. [UIView setAnimationsEnabled: NO];
  124. // TODO: extract it?
  125. ShowSplashScreen(_window);
  126. #if UNITY_SUPPORT_ROTATION
  127. // to be able to query orientation from view controller we should actually show it.
  128. // at this point we can only show splash screen, so update app orientation after we started showing it
  129. // NB: _window.rootViewController = splash view controller (not _rootController)
  130. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_window.rootViewController))];
  131. #endif
  132. NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"Unity_LoadingActivityIndicatorStyle"];
  133. ShowActivityIndicator([SplashScreen Instance], style ? [style intValue] : -1);
  134. NSNumber* vcControlled = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIViewControllerBasedStatusBarAppearance"];
  135. if (vcControlled && ![vcControlled boolValue])
  136. printf_console("\nSetting UIViewControllerBasedStatusBarAppearance to NO is no longer supported.\n"
  137. "Apple actively discourages that, and all application-wide methods of changing status bar appearance are deprecated\n\n"
  138. );
  139. }
  140. - (void)showGameUI
  141. {
  142. HideActivityIndicator();
  143. HideSplashScreen();
  144. // make sure that we start up with correctly created/inited rendering surface
  145. // NB: recreateRenderingSurface won't go into rendering because _unityAppReady is false
  146. #if UNITY_SUPPORT_ROTATION
  147. [self checkOrientationRequest];
  148. #endif
  149. [_unityView recreateRenderingSurface];
  150. // UI hierarchy
  151. [_window addSubview: _rootView];
  152. _window.rootViewController = _rootController;
  153. [_window bringSubviewToFront: _rootView];
  154. #if UNITY_SUPPORT_ROTATION
  155. // to be able to query orientation from view controller we should actually show it.
  156. // at this point we finally started to show game view controller. Just in case update orientation again
  157. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_rootController))];
  158. #endif
  159. // why we set level ready only now:
  160. // surface recreate will try to repaint if this var is set (poking unity to do it)
  161. // but this frame now is actually the first one we want to process/draw
  162. // so all the recreateSurface before now (triggered by reorientation) should simply change extents
  163. _unityAppReady = true;
  164. // why we skip present:
  165. // this will be the first frame to draw, so Start methods will be called
  166. // and we want to properly handle resolution request in Start (which might trigger surface recreate)
  167. // NB: we want to draw right after showing window, to avoid black frame creeping in
  168. _skipPresent = true;
  169. if (!UnityIsPaused())
  170. UnityRepaint();
  171. _skipPresent = false;
  172. [self repaint];
  173. [UIView setAnimationsEnabled: YES];
  174. }
  175. - (void)transitionToViewController:(UIViewController*)vc
  176. {
  177. [self willTransitionToViewController: vc fromViewController: _rootController];
  178. // first: remove from view hierarchy.
  179. // if we simply hide the window before assigning the new view controller, it will cause black frame flickering
  180. // on the other hand, hiding the window is important by itself to better signal the intent to iOS
  181. // e.g. unless we hide the window view, safeArea might stop working (due to bug in iOS if we're to speculate)
  182. // due to that we do this hide/unhide sequence: we want to to make it hidden, but still unhide it before changing window view controller.
  183. _window.hidden = YES;
  184. _window.hidden = NO;
  185. _rootController.view = nil;
  186. _window.rootViewController = nil;
  187. // second: assign new root controller (and view hierarchy with that), restore bounds
  188. _rootController = _window.rootViewController = vc;
  189. _rootController.view = _rootView;
  190. _window.bounds = [UIScreen mainScreen].bounds;
  191. // required for iOS 8, otherwise view bounds will be incorrect
  192. _rootView.bounds = _window.bounds;
  193. _rootView.center = _window.center;
  194. // third: restore window as key and layout subviews to finalize size changes
  195. [_window makeKeyAndVisible];
  196. [_window layoutSubviews];
  197. [self didTransitionToViewController: vc fromViewController: _rootController];
  198. }
  199. #if UNITY_SUPPORT_ROTATION
  200. - (void)interfaceWillChangeOrientationTo:(UIInterfaceOrientation)toInterfaceOrientation
  201. {
  202. UIInterfaceOrientation fromInterfaceOrientation = _curOrientation;
  203. _curOrientation = toInterfaceOrientation;
  204. [_unityView willRotateToOrientation: toInterfaceOrientation fromOrientation: fromInterfaceOrientation];
  205. }
  206. - (void)interfaceDidChangeOrientationFrom:(UIInterfaceOrientation)fromInterfaceOrientation
  207. {
  208. [_unityView didRotate];
  209. }
  210. #endif
  211. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  212. - (void)executeForEveryViewController:(void(^)(UIViewController*))callback
  213. {
  214. for (unsigned i = 0; i < ARRAY_SIZE(_viewControllerForOrientation); ++i)
  215. {
  216. UIViewController* vc = _viewControllerForOrientation[i];
  217. if (vc)
  218. callback(vc);
  219. }
  220. }
  221. - (void)notifyHideHomeButtonChange
  222. {
  223. // Note that we need to update all view controllers because UIKit won't necessarily
  224. // update the properties of view controllers when orientation is changed.
  225. #if PLATFORM_IOS
  226. if (@available(iOS 11.0, *))
  227. {
  228. [self executeForEveryViewController: ^(UIViewController* vc)
  229. {
  230. // setNeedsUpdateOfHomeIndicatorAutoHidden is not implemented on iOS 11.0.
  231. // The bug has been fixed in iOS 11.0.1. See http://www.openradar.me/35127134
  232. if ([vc respondsToSelector: @selector(setNeedsUpdateOfHomeIndicatorAutoHidden)])
  233. [vc setNeedsUpdateOfHomeIndicatorAutoHidden];
  234. }];
  235. }
  236. #endif
  237. }
  238. - (void)notifyDeferSystemGesturesChange
  239. {
  240. #if PLATFORM_IOS
  241. if (@available(iOS 11.0, *))
  242. {
  243. [self executeForEveryViewController: ^(UIViewController* vc)
  244. {
  245. [vc setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
  246. }];
  247. }
  248. #endif
  249. }
  250. @end
  251. #if UNITY_SUPPORT_ROTATION
  252. @implementation UnityAppController (OrientationSupport)
  253. - (UIViewController*)createRootViewControllerForOrientation:(UIInterfaceOrientation)orientation
  254. {
  255. NSAssert(orientation != 0, @"Bad UIInterfaceOrientation provided");
  256. if (_viewControllerForOrientation[orientation] == nil)
  257. _viewControllerForOrientation[orientation] = [self createUnityViewControllerForOrientation: orientation];
  258. return _viewControllerForOrientation[orientation];
  259. }
  260. - (void)checkOrientationRequest
  261. {
  262. if (!UnityHasOrientationRequest() && !UnityShouldChangeAllowedOrientations())
  263. return;
  264. // normally we want to call attemptRotationToDeviceOrientation to tell iOS that we changed orientation constraints
  265. // but if the current orientation is disabled we need special processing, as iOS will simply ignore us
  266. // the only good/robust way is to simply recreate "autorotating" view controller and transition to it if needed
  267. // please note that we want to trigger "orientation request" code path if we recreate autorotating view controller
  268. bool changeOrient = UnityHasOrientationRequest();
  269. // first we check if we need to update orientations enabled for autorotation
  270. // this needs to be done *only* if we are to continue autorotating
  271. // otherwise we will transition from this view controller
  272. // and iOS will reread enabled orientations on next ViewController activation
  273. const bool autorot = UnityShouldAutorotate();
  274. if (UnityShouldChangeAllowedOrientations() && autorot)
  275. {
  276. NSUInteger rootOrient = 1 << UIViewControllerInterfaceOrientation(self.rootViewController);
  277. if (_rootController == _viewControllerForOrientation[0] && (rootOrient & EnabledAutorotationInterfaceOrientations()))
  278. {
  279. // if we are currently autorotating AND changed allowed orientations while keeping current interface orientation allowed:
  280. // we can simply trigger attemptRotationToDeviceOrientation and we are done
  281. // please note that this can happen when current *device* orientation is disabled (and we want to enable it)
  282. [UIViewController attemptRotationToDeviceOrientation];
  283. }
  284. else
  285. {
  286. // otherwise we recreate default autorotating view controller
  287. // please note that below we will check if root controller still equals _viewControllerForOrientation[0]
  288. // in that case (we update _viewControllerForOrientation[0]) the check will fail and will trigger transition (as expected)
  289. // you may look at this check as "are we autorotating with same constraints"
  290. _viewControllerForOrientation[0] = [self createUnityViewControllerDefault];
  291. changeOrient = true;
  292. }
  293. }
  294. if (changeOrient)
  295. {
  296. // on some devices like iPhone XS layoutSubview is not called when transitioning from different orientations with the same resolution
  297. // therefore forcing layoutSubview on all orientation changes
  298. [_unityView setNeedsLayout];
  299. if (autorot)
  300. {
  301. if (_viewControllerForOrientation[0] == nil)
  302. _viewControllerForOrientation[0] = [self createUnityViewControllerDefault];
  303. if (_rootController != _viewControllerForOrientation[0])
  304. [self transitionToViewController: _viewControllerForOrientation[0]];
  305. [UIViewController attemptRotationToDeviceOrientation];
  306. }
  307. else
  308. {
  309. UIInterfaceOrientation requestedOrient = ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation());
  310. // on one hand orientInterface: should be perfectly fine "reorienting" to current orientation
  311. // in reality, ios might be confused by transitionToViewController: shenanigans coupled with "nothing have changed actually"
  312. // as an example: prior to ios12 that might result in status bar going "bad" (becoming transparent)
  313. if (_rootController != _viewControllerForOrientation[requestedOrient])
  314. [self orientInterface: requestedOrient];
  315. }
  316. }
  317. UnityOrientationRequestWasCommitted();
  318. }
  319. - (void)orientInterface:(UIInterfaceOrientation)orient
  320. {
  321. if (_unityAppReady)
  322. UnityFinishRendering();
  323. [KeyboardDelegate StartReorientation];
  324. [CATransaction begin];
  325. {
  326. UIInterfaceOrientation oldOrient = _curOrientation;
  327. UIInterfaceOrientation newOrient = orient;
  328. [self interfaceWillChangeOrientationTo: newOrient];
  329. [self transitionToViewController: [self createRootViewControllerForOrientation: newOrient]];
  330. [self interfaceDidChangeOrientationFrom: oldOrient];
  331. [UIApplication sharedApplication].statusBarOrientation = orient;
  332. }
  333. [CATransaction commit];
  334. [KeyboardDelegate FinishReorientation];
  335. }
  336. - (void)orientUnity:(UIInterfaceOrientation)orient
  337. {
  338. [self orientInterface: orient];
  339. }
  340. @end
  341. #endif
  342. extern "C" void UnityNotifyHideHomeButtonChange()
  343. {
  344. [GetAppController() notifyHideHomeButtonChange];
  345. }
  346. extern "C" void UnityNotifyDeferSystemGesturesChange()
  347. {
  348. [GetAppController() notifyDeferSystemGesturesChange];
  349. }