UnityAppController+ViewHandling.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #if UNITY_SUPPORT_ROTATION
  74. - (UIViewController*)createSecondaryAutorotatingViewController
  75. {
  76. if (_secondaryAutorotatingViewController == nil)
  77. _secondaryAutorotatingViewController = [self createUnityViewControllerDefault];
  78. std::swap(_viewControllerForOrientation[0], _secondaryAutorotatingViewController);
  79. return _viewControllerForOrientation[0];
  80. }
  81. #endif
  82. - (UIViewController*)topMostController
  83. {
  84. UIViewController *topController = self.window.rootViewController;
  85. while (topController.presentedViewController)
  86. topController = topController.presentedViewController;
  87. return topController;
  88. }
  89. - (void)willStartWithViewController:(UIViewController*)controller
  90. {
  91. _unityView.contentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  92. _unityView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  93. _rootController.view = _rootView = _unityView;
  94. }
  95. - (void)willTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  96. {
  97. }
  98. - (UIView*)createSnapshotView
  99. {
  100. // Snapshot API appeared on iOS 7, however before iOS 8 tweaking hierarchy like that on going to
  101. // background results in all kind of weird things when going back to foreground so we do snapshotting
  102. // only on iOS 8 and newer.
  103. // Note that on iPads with iOS 9 or later (up to iOS 10.2 at least) there's a bug in the iOS
  104. // compositor: any use of -[UIView snapshotViewAfterScreenUpdates] causes black screen being shown
  105. // temporarily when 4 finger gesture to swipe to another app in the task switcher is being performed slowly
  106. #if UNITY_SNAPSHOT_VIEW_ON_APPLICATION_PAUSE
  107. return _ios80orNewer ? [_rootView snapshotViewAfterScreenUpdates: YES] : nil;
  108. #else
  109. return nil;
  110. #endif
  111. }
  112. - (void)createUI
  113. {
  114. NSAssert(_unityView != nil, @"_unityView should be inited at this point");
  115. NSAssert(_window != nil, @"_window should be inited at this point");
  116. _rootController = [self createRootViewController];
  117. [self willStartWithViewController: _rootController];
  118. NSAssert(_rootView != nil, @"_rootView should be inited at this point");
  119. NSAssert(_rootController != nil, @"_rootController should be inited at this point");
  120. [_window makeKeyAndVisible];
  121. [UIView setAnimationsEnabled: NO];
  122. // TODO: extract it?
  123. ShowSplashScreen(_window);
  124. #if UNITY_SUPPORT_ROTATION
  125. // to be able to query orientation from view controller we should actually show it.
  126. // at this point we can only show splash screen, so update app orientation after we started showing it
  127. // NB: _window.rootViewController = splash view controller (not _rootController)
  128. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_window.rootViewController))];
  129. #endif
  130. NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"Unity_LoadingActivityIndicatorStyle"];
  131. ShowActivityIndicator([SplashScreen Instance], style ? [style intValue] : -1);
  132. }
  133. - (void)showGameUI
  134. {
  135. HideActivityIndicator();
  136. HideSplashScreen();
  137. // make sure that we start up with correctly created/inited rendering surface
  138. // NB: recreateRenderingSurface won't go into rendering because _unityAppReady is false
  139. #if UNITY_SUPPORT_ROTATION
  140. [self checkOrientationRequest];
  141. #endif
  142. [_unityView recreateRenderingSurface];
  143. // UI hierarchy
  144. [_window addSubview: _rootView];
  145. _window.rootViewController = _rootController;
  146. [_window bringSubviewToFront: _rootView];
  147. #if UNITY_SUPPORT_ROTATION
  148. // to be able to query orientation from view controller we should actually show it.
  149. // at this point we finally started to show game view controller. Just in case update orientation again
  150. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_rootController))];
  151. #endif
  152. // why we set level ready only now:
  153. // surface recreate will try to repaint if this var is set (poking unity to do it)
  154. // but this frame now is actually the first one we want to process/draw
  155. // so all the recreateSurface before now (triggered by reorientation) should simply change extents
  156. _unityAppReady = true;
  157. // why we skip present:
  158. // this will be the first frame to draw, so Start methods will be called
  159. // and we want to properly handle resolution request in Start (which might trigger surface recreate)
  160. // NB: we want to draw right after showing window, to avoid black frame creeping in
  161. _skipPresent = true;
  162. if (!UnityIsPaused())
  163. UnityRepaint();
  164. _skipPresent = false;
  165. [self repaint];
  166. [UIView setAnimationsEnabled: YES];
  167. }
  168. - (void)transitionToViewController:(UIViewController*)vc
  169. {
  170. [self willTransitionToViewController: vc fromViewController: _rootController];
  171. // first: remove from view hierarchy.
  172. // if we simply hide the window before assigning the new view controller, it will cause black frame flickering
  173. // on the other hand, hiding the window is important by itself to better signal the intent to iOS
  174. // e.g. unless we hide the window view, safeArea might stop working (due to bug in iOS if we're to speculate)
  175. // 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.
  176. _window.hidden = YES;
  177. _window.hidden = NO;
  178. _rootController.view = nil;
  179. _window.rootViewController = nil;
  180. // second: assign new root controller (and view hierarchy with that), restore bounds
  181. _rootController = _window.rootViewController = vc;
  182. _rootController.view = _rootView;
  183. _window.bounds = [UIScreen mainScreen].bounds;
  184. // required for iOS 8, otherwise view bounds will be incorrect
  185. _rootView.bounds = _window.bounds;
  186. _rootView.center = _window.center;
  187. // third: restore window as key and layout subviews to finalize size changes
  188. [_window makeKeyAndVisible];
  189. [_window layoutSubviews];
  190. }
  191. #if UNITY_SUPPORT_ROTATION
  192. - (void)interfaceWillChangeOrientationTo:(UIInterfaceOrientation)toInterfaceOrientation
  193. {
  194. UIInterfaceOrientation fromInterfaceOrientation = _curOrientation;
  195. _curOrientation = toInterfaceOrientation;
  196. [_unityView willRotateToOrientation: toInterfaceOrientation fromOrientation: fromInterfaceOrientation];
  197. }
  198. - (void)interfaceDidChangeOrientationFrom:(UIInterfaceOrientation)fromInterfaceOrientation
  199. {
  200. [_unityView didRotate];
  201. }
  202. #endif
  203. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  204. - (void)executeForEveryViewController:(void(^)(UIViewController*))callback
  205. {
  206. for (unsigned i = 0; i < ARRAY_SIZE(_viewControllerForOrientation); ++i)
  207. {
  208. UIViewController* vc = _viewControllerForOrientation[i];
  209. if (vc)
  210. callback(vc);
  211. }
  212. #if UNITY_SUPPORT_ROTATION
  213. if (_secondaryAutorotatingViewController)
  214. callback(_secondaryAutorotatingViewController);
  215. #endif
  216. }
  217. - (void)notifyHideHomeButtonChange
  218. {
  219. // Note that we need to update all view controllers because UIKit won't necessarily
  220. // update the properties of view controllers when orientation is changed.
  221. #if PLATFORM_IOS && UNITY_HAS_IOSSDK_11_0
  222. if (@available(iOS 11.0, *))
  223. {
  224. [self executeForEveryViewController: ^(UIViewController* vc)
  225. {
  226. // setNeedsUpdateOfHomeIndicatorAutoHidden is not implemented on iOS 11.0.
  227. // The bug has been fixed in iOS 11.0.1. See http://www.openradar.me/35127134
  228. if ([vc respondsToSelector: @selector(setNeedsUpdateOfHomeIndicatorAutoHidden)])
  229. [vc setNeedsUpdateOfHomeIndicatorAutoHidden];
  230. }];
  231. }
  232. #endif
  233. }
  234. - (void)notifyDeferSystemGesturesChange
  235. {
  236. #if PLATFORM_IOS && UNITY_HAS_IOSSDK_11_0
  237. if (@available(iOS 11.0, *))
  238. {
  239. [self executeForEveryViewController: ^(UIViewController* vc)
  240. {
  241. [vc setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
  242. }];
  243. }
  244. #endif
  245. }
  246. @end
  247. #if UNITY_SUPPORT_ROTATION
  248. @implementation UnityAppController (OrientationSupport)
  249. - (UIViewController*)createRootViewControllerForOrientation:(UIInterfaceOrientation)orientation
  250. {
  251. NSAssert(orientation != 0, @"Bad UIInterfaceOrientation provided");
  252. if (_viewControllerForOrientation[orientation] == nil)
  253. _viewControllerForOrientation[orientation] = [self createUnityViewControllerForOrientation: orientation];
  254. return _viewControllerForOrientation[orientation];
  255. }
  256. - (void)forceAutorotatingControllerToRefreshEnabledOrientationsIfNeeded
  257. {
  258. if (!UnityShouldAutorotate())
  259. return;
  260. // compare unity enabled orientation with current rootViewController orientation
  261. NSUInteger rootOrient = 1 << UIViewControllerInterfaceOrientation(self.rootViewController);
  262. // normally we want to call attemptRotationToDeviceOrientation to tell iOS that we changed orientation constraints
  263. // but if the current orientation is disabled we need special processing, as iOS will simply ignore us
  264. if (rootOrient & EnabledAutorotationInterfaceOrientations())
  265. [UIViewController attemptRotationToDeviceOrientation];
  266. else
  267. [self transitionToViewController: [self createSecondaryAutorotatingViewController]];
  268. }
  269. - (void)checkOrientationRequest
  270. {
  271. if (UnityHasOrientationRequest())
  272. {
  273. if (UnityShouldAutorotate())
  274. {
  275. if (_rootController != _viewControllerForOrientation[0])
  276. {
  277. [self transitionToViewController: [self createRootViewController]];
  278. [UIViewController attemptRotationToDeviceOrientation];
  279. }
  280. else
  281. [self forceAutorotatingControllerToRefreshEnabledOrientationsIfNeeded];
  282. }
  283. else
  284. {
  285. ScreenOrientation requestedOrient = (ScreenOrientation)UnityRequestedScreenOrientation();
  286. [self orientInterface: ConvertToIosScreenOrientation(requestedOrient)];
  287. }
  288. UnityOrientationRequestWasCommitted();
  289. }
  290. }
  291. - (void)orientInterface:(UIInterfaceOrientation)orient
  292. {
  293. if (_curOrientation == orient && _rootController != _viewControllerForOrientation[0])
  294. return;
  295. if (_unityAppReady)
  296. UnityFinishRendering();
  297. [KeyboardDelegate StartReorientation];
  298. [CATransaction begin];
  299. {
  300. UIInterfaceOrientation oldOrient = _curOrientation;
  301. UIInterfaceOrientation newOrient = orient;
  302. [self interfaceWillChangeOrientationTo: newOrient];
  303. [self transitionToViewController: [self createRootViewControllerForOrientation: newOrient]];
  304. [self interfaceDidChangeOrientationFrom: oldOrient];
  305. [UIApplication sharedApplication].statusBarOrientation = orient;
  306. }
  307. [CATransaction commit];
  308. [KeyboardDelegate FinishReorientation];
  309. }
  310. - (void)orientUnity:(UIInterfaceOrientation)orient
  311. {
  312. [self orientInterface: orient];
  313. }
  314. @end
  315. #endif
  316. extern "C" void UnityNotifyHideHomeButtonChange()
  317. {
  318. [GetAppController() notifyHideHomeButtonChange];
  319. }
  320. extern "C" void UnityNotifyDeferSystemGesturesChange()
  321. {
  322. [GetAppController() notifyDeferSystemGesturesChange];
  323. }