UnityAppController+ViewHandling.mm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/Keyboard.h"
  10. #include <utility>
  11. extern bool _skipPresent;
  12. extern bool _unityAppReady;
  13. @implementation UnityAppController (ViewHandling)
  14. #if UNITY_SUPPORT_ROTATION
  15. // special case for when we DO know the app orientation, but dont get it through normal mechanism (UIViewController orientation handling)
  16. // how can this happen:
  17. // 1. On startup: ios is not sending "change orientation" notifications on startup (but rather we "start" in correct one already)
  18. // 2. When using presentation controller it can override orientation constraints, so on dismissing we need to tweak app orientation;
  19. // pretty much like startup situation UIViewController would have correct orientation, and app will be out-of-sync
  20. - (void)updateAppOrientation:(UIInterfaceOrientation)orientation
  21. {
  22. // update our (AppContoller) view of orientation
  23. _curOrientation = orientation;
  24. // do unity view "orientation magic"
  25. [_unityView willRotateToOrientation: orientation fromOrientation: (UIInterfaceOrientation)UIInterfaceOrientationUnknown];
  26. [_unityView didRotate];
  27. // after we have updated unity view, this will poke unity itself about the changes in orient/extents
  28. [_unityView boundsUpdated];
  29. }
  30. #endif
  31. - (UnityView*)createUnityView
  32. {
  33. return [[UnityView alloc] initFromMainScreen];
  34. }
  35. - (UIViewController*)createUnityViewControllerDefault
  36. {
  37. UnityViewControllerBase* ret = [AllocUnityDefaultViewController() init];
  38. ret.notificationDelegate = [[UnityViewControllerNotificationsDefaultSender alloc] init];
  39. #if PLATFORM_TVOS
  40. ret.controllerUserInteractionEnabled = YES;
  41. #endif
  42. return ret;
  43. }
  44. #if UNITY_SUPPORT_ROTATION
  45. - (UIViewController*)createUnityViewControllerForOrientation:(UIInterfaceOrientation)orient
  46. {
  47. UnityViewControllerBase* ret = [AllocUnitySingleOrientationViewController(orient) init];
  48. ret.notificationDelegate = [[UnityViewControllerNotificationsDefaultSender alloc] init];
  49. return ret;
  50. }
  51. #endif
  52. - (UIViewController*)createRootViewController
  53. {
  54. UIViewController* ret = nil;
  55. if (!UNITY_SUPPORT_ROTATION || UnityShouldAutorotate())
  56. ret = [self createUnityViewControllerDefault];
  57. #if UNITY_SUPPORT_ROTATION
  58. if (ret == nil)
  59. ret = [self createRootViewControllerForOrientation: ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation())];
  60. #endif
  61. return ret;
  62. }
  63. - (UIViewController*)topMostController
  64. {
  65. UIViewController *topController = self.window.rootViewController;
  66. while (topController.presentedViewController)
  67. topController = topController.presentedViewController;
  68. return topController;
  69. }
  70. - (void)willStartWithViewController:(UIViewController*)controller
  71. {
  72. #if !PLATFORM_VISIONOS
  73. _unityView.contentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  74. #else
  75. _unityView.contentScaleFactor = 1.0f;
  76. #endif
  77. _unityView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  78. _rootController.view = _rootView = _unityView;
  79. }
  80. - (void)willTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  81. {
  82. }
  83. - (void)didTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  84. {
  85. #if UNITY_SUPPORT_ROTATION && !PLATFORM_VISIONOS
  86. // when transitioning between view controllers ios will not send reorient events (because they are bound to controllers, not view)
  87. // so we imitate them here so unity view can update its size/orientation
  88. UIInterfaceOrientation newOrientation = UIViewControllerInterfaceOrientation(toController);
  89. [_unityView willRotateToOrientation: newOrientation fromOrientation: ConvertToIosScreenOrientation(_unityView.contentOrientation)];
  90. [_unityView didRotate];
  91. // 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)
  92. _curOrientation = newOrientation;
  93. #endif
  94. }
  95. - (UIView*)createSnapshotView
  96. {
  97. // 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]
  98. // causes black screen being shown temporarily when 4 finger gesture to swipe to another app in the task switcher is being performed slowly
  99. return [_rootView snapshotViewAfterScreenUpdates: YES];
  100. }
  101. - (void)createUI
  102. {
  103. NSAssert(_unityView != nil, @"_unityView should be inited at this point");
  104. NSAssert(_window != nil, @"_window should be inited at this point");
  105. _rootController = [self createRootViewController];
  106. [self willStartWithViewController: _rootController];
  107. NSAssert(_rootView != nil, @"_rootView should be inited at this point");
  108. NSAssert(_rootController != nil, @"_rootController should be inited at this point");
  109. // We need to add the root view to the view hierarchy before initializing graphics,
  110. // as plugins might need to access view properties (e.g. safeAreaInsets). Otherwise,
  111. // they will get default values if the view is not yet added to the window.
  112. [_window addSubview: _rootView];
  113. // We should have rootViewController set always, otherwise UIKit might trow exception when doing anything with UI
  114. _window.rootViewController = _rootController;
  115. [UIView setAnimationsEnabled: NO];
  116. // make window visible only after we have set up initial controller we want to show
  117. [_window makeKeyAndVisible];
  118. #if UNITY_SUPPORT_ROTATION
  119. // to be able to query orientation from view controller we should actually show it.
  120. // at this point we can only show splash screen, so update app orientation after we started showing it
  121. // NB: _window.rootViewController = splash view controller (not _rootController)
  122. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_window.rootViewController))];
  123. #endif
  124. NSNumber* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"Unity_LoadingActivityIndicatorStyle"];
  125. ShowActivityIndicator(_rootView, style ? [style intValue] : -1);
  126. NSNumber* vcControlled = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIViewControllerBasedStatusBarAppearance"];
  127. if (vcControlled && ![vcControlled boolValue])
  128. printf_console("\nSetting UIViewControllerBasedStatusBarAppearance to NO is no longer supported.\n"
  129. "Apple actively discourages that, and all application-wide methods of changing status bar appearance are deprecated\n\n"
  130. );
  131. }
  132. - (void)showGameUI
  133. {
  134. HideActivityIndicator();
  135. // make sure that we start up with correctly created/inited rendering surface
  136. // NB: recreateRenderingSurface won't go into rendering because _unityAppReady is false
  137. #if UNITY_SUPPORT_ROTATION
  138. [self checkOrientationRequest];
  139. #endif
  140. [_unityView recreateRenderingSurface];
  141. // UI hierarchy
  142. _window.rootViewController = _rootController;
  143. [_window bringSubviewToFront: _rootView];
  144. #if UNITY_SUPPORT_ROTATION
  145. // to be able to query orientation from view controller we should actually show it.
  146. // at this point we finally started to show game view controller. Just in case update orientation again
  147. [self updateAppOrientation: ConvertToIosScreenOrientation(UIViewControllerOrientation(_rootController))];
  148. #endif
  149. // why we set level ready only now:
  150. // surface recreate will try to repaint if this var is set (poking unity to do it)
  151. // but this frame now is actually the first one we want to process/draw
  152. // so all the recreateSurface before now (triggered by reorientation) should simply change extents
  153. _unityAppReady = true;
  154. // why we skip present:
  155. // this will be the first frame to draw, so Start methods will be called
  156. // and we want to properly handle resolution request in Start (which might trigger surface recreate)
  157. // NB: we want to draw right after showing window, to avoid black frame creeping in
  158. _skipPresent = true;
  159. if (!UnityIsPaused())
  160. UnityRepaint();
  161. _skipPresent = false;
  162. [self repaint];
  163. [UIView setAnimationsEnabled: YES];
  164. }
  165. #if UNITY_SUPPORT_ROTATION
  166. - (void)transitionToViewController:(UIViewController*)vc
  167. {
  168. [self willTransitionToViewController: vc fromViewController: _rootController];
  169. // first: remove from view hierarchy.
  170. // if we simply hide the window before assigning the new view controller, it will cause black frame flickering
  171. // on the other hand, hiding the window is important by itself to better signal the intent to iOS
  172. // e.g. unless we hide the window view, safeArea might stop working (due to bug in iOS if we're to speculate)
  173. // 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.
  174. _window.hidden = YES;
  175. _window.hidden = NO;
  176. _rootController.view = nil;
  177. _window.rootViewController = nil;
  178. // second: assign new root controller (and view hierarchy with that), restore bounds
  179. // this is very important to first set _rootController, and only then window root controller
  180. // as the latter will poke [UIApplicationDelegate application:supportedInterfaceOrientationsForWindow:]
  181. // and unity implementation expects _rootController to be already set
  182. _window.rootViewController = _rootController = vc;
  183. _rootController.view = _rootView;
  184. // CODE ARCHEOLOGY: in here we were tweaking window bounds to agree with screen bounds (and did some iOS8 specific workaround)
  185. // This is no longer needed it seems, and is actually harmful for the "split view" supporting apps
  186. // If you have fullscreen window, it will be automatically resized to take the whole screen
  187. // and otherwise we must not touch it, as it will be controlled by multitasking
  188. // third: restore window as key and layout subviews to finalize size changes
  189. [_window makeKeyAndVisible];
  190. [_window layoutSubviews];
  191. // In iOS16+ after we setup a new contoller and when we have multiple windows visible, iOS not fully prepares
  192. // view controller according it's orientation requirements. And then inside didTransitionToViewController:
  193. // from UIViewControllerInterfaceOrientation we get bad orientation as it uses scree.coordinationSpace which is not
  194. // yet changed. So we want to delay didTransitionToViewController call. And in this case we get a call to view
  195. // controllers -viewWillTransitionToSize: method and at this time the orientation change is already happened and
  196. // then we send didTransitionToViewController. If view controller changes are setup correctly from iOS, then iOS do
  197. // not call -viewWillTransitionToSize:.
  198. UIInterfaceOrientation newOrientation = UIViewControllerInterfaceOrientation(vc);
  199. BOOL orientationChangedToSupported = vc.supportedInterfaceOrientations & (1 << newOrientation);
  200. if (!UnityiOS160orNewer() || orientationChangedToSupported)
  201. {
  202. [self didTransitionToViewController: vc fromViewController: _rootController];
  203. }
  204. }
  205. - (void)interfaceWillChangeOrientationTo:(UIInterfaceOrientation)toInterfaceOrientation
  206. {
  207. UIInterfaceOrientation fromInterfaceOrientation = _curOrientation;
  208. _curOrientation = toInterfaceOrientation;
  209. [_unityView willRotateToOrientation: toInterfaceOrientation fromOrientation: fromInterfaceOrientation];
  210. }
  211. - (void)interfaceDidChangeOrientationFrom:(UIInterfaceOrientation)fromInterfaceOrientation
  212. {
  213. [_unityView didRotate];
  214. }
  215. #endif
  216. - (void)notifyHideHomeButtonChange
  217. {
  218. #if PLATFORM_IOS || PLATFORM_VISIONOS
  219. // setNeedsUpdateOfHomeIndicatorAutoHidden is not implemented on iOS 11.0.
  220. // The bug has been fixed in iOS 11.0.1. See http://www.openradar.me/35127134
  221. if ([_rootController respondsToSelector: @selector(setNeedsUpdateOfHomeIndicatorAutoHidden)])
  222. [_rootController setNeedsUpdateOfHomeIndicatorAutoHidden];
  223. #endif
  224. }
  225. - (void)notifyDeferSystemGesturesChange
  226. {
  227. #if PLATFORM_IOS || PLATFORM_VISIONOS
  228. [_rootController setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
  229. #endif
  230. }
  231. @end
  232. #if UNITY_SUPPORT_ROTATION
  233. @implementation UnityAppController (OrientationSupport)
  234. - (UIViewController*)createRootViewControllerForOrientation:(UIInterfaceOrientation)orientation
  235. {
  236. return [self createUnityViewControllerForOrientation: orientation];
  237. }
  238. - (void)checkOrientationRequest
  239. {
  240. // if no orientation/allowed-orientation change - do nothing
  241. if (!UnityHasOrientationRequest() && !UnityShouldChangeAllowedOrientations())
  242. return;
  243. // if there is a presentation controller, it takes over orientation control
  244. // in this case we should completely ignore all orientation changes
  245. // mind you, we just *delay* them, and they will be satisfied once presentation controller is dismissed
  246. // extra care like this is needed, because below we might recreate ViewController completely breaking
  247. // presentation controller dismissal
  248. if (_rootController.presentedViewController)
  249. return;
  250. // normally we want to call attemptRotationToDeviceOrientation to tell iOS that we changed orientation constraints
  251. // but if the current orientation is disabled we need special processing, as iOS will simply ignore us
  252. // the only good/robust way is to simply recreate "autorotating" view controller and transition to it if needed
  253. // please note that we want to trigger "orientation request" code path if we recreate autorotating view controller
  254. bool changeOrient = UnityHasOrientationRequest();
  255. // if we should recreate autorotating view controller - see below
  256. bool shouldTransferToNewAutorotVC = false;
  257. // first we check if we need to update orientations enabled for autorotation
  258. // this needs to be done *only* if we are to continue autorotating
  259. // otherwise we will transition from this view controller
  260. // and iOS will reread enabled orientations on next ViewController activation
  261. const bool autorot = UnityShouldAutorotate(), autorotChanged = UnityAutorotationStatusChanged();
  262. if (UnityShouldChangeAllowedOrientations() && autorot)
  263. {
  264. NSUInteger rootOrient = 1 << UIViewControllerInterfaceOrientation(self.rootViewController);
  265. if (!autorotChanged && (rootOrient & EnabledAutorotationInterfaceOrientations()))
  266. {
  267. // instead of querying unity for supported orientations, we keep them in the default (autorotating) controller
  268. // this is THE place where we should update those (otherwise, filled on creation)
  269. if ([self.rootViewController isKindOfClass: [UnityDefaultViewController class]])
  270. [(UnityDefaultViewController*)self.rootViewController updateSupportedOrientations];
  271. // if we are currently autorotating AND changed allowed orientations while keeping current interface orientation allowed:
  272. // we can simply trigger attemptRotationToDeviceOrientation and we are done
  273. // please note that this can happen when current *device* orientation is disabled (and we want to enable it)
  274. [UIViewController attemptRotationToDeviceOrientation];
  275. }
  276. else
  277. {
  278. // otherwise we recreate default autorotating view controller
  279. // to spell it out, we recreate if:
  280. // - we continue doing autorotation, but the current orientation is disabled
  281. // - we were not autorotating but start now
  282. shouldTransferToNewAutorotVC = true;
  283. changeOrient = true;
  284. }
  285. }
  286. if (changeOrient)
  287. {
  288. // on some devices like iPhone XS layoutSubview is not called when transitioning from different orientations with the same resolution
  289. // therefore forcing layoutSubview on all orientation changes
  290. [_unityView setNeedsLayout];
  291. if (autorot)
  292. {
  293. // just started autorotating or decided to recreate autorot controller above
  294. if (autorotChanged || shouldTransferToNewAutorotVC)
  295. [self transitionToViewController: [self createUnityViewControllerDefault]];
  296. [UIViewController attemptRotationToDeviceOrientation];
  297. }
  298. else
  299. {
  300. UIInterfaceOrientation requestedOrient = ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation());
  301. // on one hand orientInterface: should be perfectly fine "reorienting" to current orientation
  302. // in reality, ios might be confused by transitionToViewController: shenanigans coupled with "nothing have changed actually"
  303. // as an example: prior to ios12 that might result in status bar going "bad" (becoming transparent)
  304. // NOTE: if we have switched from autorotation to fixed orientation, we must do the switch to pick new VC
  305. if (_curOrientation != requestedOrient || autorotChanged)
  306. [self orientInterface: requestedOrient];
  307. }
  308. }
  309. UnityOrientationRequestWasCommitted();
  310. }
  311. - (void)orientInterface:(UIInterfaceOrientation)orient
  312. {
  313. if (_unityAppReady)
  314. UnityFinishRendering();
  315. [KeyboardDelegate StartReorientation];
  316. [CATransaction begin];
  317. {
  318. UIInterfaceOrientation oldOrient = _curOrientation;
  319. UIInterfaceOrientation newOrient = orient;
  320. [self interfaceWillChangeOrientationTo: newOrient];
  321. [self transitionToViewController: [self createRootViewControllerForOrientation: newOrient]];
  322. [self interfaceDidChangeOrientationFrom: oldOrient];
  323. #if !PLATFORM_VISIONOS
  324. [UIApplication sharedApplication].statusBarOrientation = orient;
  325. #endif
  326. }
  327. [CATransaction commit];
  328. [KeyboardDelegate FinishReorientation];
  329. }
  330. - (void)orientUnity:(UIInterfaceOrientation)orient
  331. {
  332. [self orientInterface: orient];
  333. }
  334. @end
  335. #endif
  336. extern "C" void UnityNotifyHideHomeButtonChange()
  337. {
  338. [GetAppController() notifyHideHomeButtonChange];
  339. }
  340. extern "C" void UnityNotifyDeferSystemGesturesChange()
  341. {
  342. [GetAppController() notifyDeferSystemGesturesChange];
  343. }