UnityAppController+ViewHandling.mm 15 KB

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