暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UnityAppController+ViewHandling.mm 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. @implementation UnityAppController (ViewHandling)
  13. #if UNITY_SUPPORT_ROTATION
  14. // special case for when we DO know the app orientation, but dont get it through normal mechanism (UIViewController orientation handling)
  15. // how can this happen:
  16. // 1. On startup: ios is not sending "change orientation" notifications on startup (but rather we "start" in correct one already)
  17. // 2. When using presentation controller it can override orientation constraints, so on dismissing we need to tweak app orientation;
  18. // pretty much like startup situation UIViewController would have correct orientation, and app will be out-of-sync
  19. - (void)updateAppOrientation:(UIInterfaceOrientation)orientation
  20. {
  21. // update our (AppContoller) view of orientation
  22. _curOrientation = orientation;
  23. // do unity view "orientation magic"
  24. [_unityView willRotateToOrientation: orientation fromOrientation: (UIInterfaceOrientation)UIInterfaceOrientationUnknown];
  25. [_unityView didRotate];
  26. // after we have updated unity view, this will poke unity itself about the changes in orient/extents
  27. [_unityView boundsUpdated];
  28. }
  29. #endif
  30. - (UnityView*)createUnityView
  31. {
  32. return [[UnityView alloc] initFromMainScreen];
  33. }
  34. - (UIViewController*)createUnityViewControllerDefault
  35. {
  36. UnityViewControllerBase* ret = [AllocUnityDefaultViewController() init];
  37. ret.notificationDelegate = [[UnityViewControllerNotificationsDefaultSender alloc] init];
  38. #if PLATFORM_TVOS
  39. ret.controllerUserInteractionEnabled = YES;
  40. #endif
  41. return ret;
  42. }
  43. #if UNITY_SUPPORT_ROTATION
  44. - (UIViewController*)createUnityViewControllerForOrientation:(UIInterfaceOrientation)orient
  45. {
  46. UnityViewControllerBase* ret = [AllocUnitySingleOrientationViewController(orient) init];
  47. ret.notificationDelegate = [[UnityViewControllerNotificationsDefaultSender alloc] init];
  48. return ret;
  49. }
  50. #endif
  51. - (UIViewController*)createRootViewController
  52. {
  53. UIViewController* ret = nil;
  54. if (!UNITY_SUPPORT_ROTATION || UnityShouldAutorotate())
  55. ret = [self createUnityViewControllerDefault];
  56. #if UNITY_SUPPORT_ROTATION
  57. if (ret == nil)
  58. ret = [self createRootViewControllerForOrientation: ConvertToIosScreenOrientation((ScreenOrientation)UnityRequestedScreenOrientation())];
  59. #endif
  60. return ret;
  61. }
  62. - (UIViewController*)topMostController
  63. {
  64. UIViewController *topController = self.window.rootViewController;
  65. while (topController.presentedViewController)
  66. topController = topController.presentedViewController;
  67. return topController;
  68. }
  69. - (void)willStartWithViewController:(UIViewController*)controller
  70. {
  71. #if !PLATFORM_VISIONOS
  72. _unityView.contentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  73. #else
  74. _unityView.contentScaleFactor = 1.0f;
  75. #endif
  76. _unityView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  77. _rootController.view = _rootView = _unityView;
  78. }
  79. - (void)willTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  80. {
  81. }
  82. - (void)didTransitionToViewController:(UIViewController*)toController fromViewController:(UIViewController*)fromController
  83. {
  84. #if UNITY_SUPPORT_ROTATION && !PLATFORM_VISIONOS
  85. // when transitioning between view controllers ios will not send reorient events (because they are bound to controllers, not view)
  86. // so we imitate them here so unity view can update its size/orientation
  87. UIInterfaceOrientation newOrientation = UIViewControllerInterfaceOrientation(toController);
  88. [_unityView willRotateToOrientation:newOrientation fromOrientation: ConvertToIosScreenOrientation(_unityView.contentOrientation)];
  89. [_unityView didRotate];
  90. // 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)
  91. _curOrientation = newOrientation;
  92. #endif
  93. }
  94. - (UIView*)createSnapshotView
  95. {
  96. // 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]
  97. // causes black screen being shown temporarily when 4 finger gesture to swipe to another app in the task switcher is being performed slowly
  98. return [_rootView snapshotViewAfterScreenUpdates: YES];
  99. }
  100. - (void)createUI
  101. {
  102. NSAssert(_unityView != nil, @"_unityView should be inited at this point");
  103. NSAssert(_window != nil, @"_window should be inited at this point");
  104. _rootController = [self createRootViewController];
  105. [self willStartWithViewController: _rootController];
  106. NSAssert(_rootView != nil, @"_rootView should be inited at this point");
  107. NSAssert(_rootController != nil, @"_rootController should be inited at this point");
  108. // CODE ARCHEOLOGY: We used to add _rootView (unityView) to the subviews of _window so that unityView would get its
  109. // initial actual device values (e.g. safeAreaInsets) before initializing graphics. This is not needed anymore after
  110. // we made a change where iOS is handling splash screen. Now unityView will be configured at
  111. // [_window makeKeyAndVisible] call. makeKeyAndVisible will configure _window.rootViewController.view, which is
  112. // _rootController.view, which is unityView (_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 AppReady state is not set
  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. [self advanceEngineLoadState: kUnityEngineLoadStateAppReady];
  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 (self.engineLoadState >= kUnityEngineLoadStateAppReady)
  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. #pragma clang diagnostic push
  325. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  326. // this was deprecated in favor of [UIWindowScene setInterfaceOrientation:]
  327. // this API works perfectly fine for now, so we use it until we rewrite/modernize trampoline to be Scene-based
  328. [UIApplication sharedApplication].statusBarOrientation = orient;
  329. #pragma clang diagnostic pop
  330. #endif
  331. }
  332. [CATransaction commit];
  333. [KeyboardDelegate FinishReorientation];
  334. }
  335. - (void)orientUnity:(UIInterfaceOrientation)orient
  336. {
  337. [self orientInterface: orient];
  338. }
  339. @end
  340. #endif
  341. extern "C" void UnityNotifyHideHomeButtonChange()
  342. {
  343. [GetAppController() notifyHideHomeButtonChange];
  344. }
  345. extern "C" void UnityNotifyDeferSystemGesturesChange()
  346. {
  347. [GetAppController() notifyDeferSystemGesturesChange];
  348. }