123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- #if PLATFORM_IOS
- #import "UnityViewControllerBase.h"
- #import "UnityAppController.h"
- #include "OrientationSupport.h"
- #include "Keyboard.h"
- #include "UnityView.h"
- #include "PluginBase/UnityViewControllerListener.h"
- #include "UnityAppController.h"
- #include "UnityAppController+ViewHandling.h"
- #include "Unity/ObjCRuntime.h"
- // when returning from presenting UIViewController we might need to update app orientation to "correct" one, as we wont get rotation notification
- @interface UnityAppController ()
- - (void)updateAppOrientation:(UIInterfaceOrientation)orientation;
- @end
- #ifndef __IPHONE_16_0
- @interface UIViewController ()
- - (void)setNeedsUpdateOfSupportedInterfaceOrientations;
- @end
- #endif
- @implementation UnityViewControllerBase (iOS)
- - (BOOL)shouldAutorotate
- {
- return YES;
- }
- - (BOOL)prefersStatusBarHidden
- {
- static bool _PrefersStatusBarHidden = true;
- static bool _PrefersStatusBarHiddenInited = false;
- if (!_PrefersStatusBarHiddenInited)
- {
- NSNumber* hidden = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarHidden"];
- _PrefersStatusBarHidden = hidden ? [hidden boolValue] : YES;
- _PrefersStatusBarHiddenInited = true;
- }
- return _PrefersStatusBarHidden;
- }
- - (UIStatusBarStyle)preferredStatusBarStyle
- {
- static UIStatusBarStyle _PreferredStatusBarStyle = UIStatusBarStyleDefault;
- static bool _PreferredStatusBarStyleInited = false;
- if (!_PreferredStatusBarStyleInited)
- {
- NSString* style = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UIStatusBarStyle"];
- if (style && [style isEqualToString: @"UIStatusBarStyleLightContent"])
- _PreferredStatusBarStyle = UIStatusBarStyleLightContent;
- _PreferredStatusBarStyleInited = true;
- }
- return _PreferredStatusBarStyle;
- }
- - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
- {
- UIRectEdge res = UIRectEdgeNone;
- if (UnityGetDeferSystemGesturesTopEdge())
- res |= UIRectEdgeTop;
- if (UnityGetDeferSystemGesturesBottomEdge())
- res |= UIRectEdgeBottom;
- if (UnityGetDeferSystemGesturesLeftEdge())
- res |= UIRectEdgeLeft;
- if (UnityGetDeferSystemGesturesRightEdge())
- res |= UIRectEdgeRight;
- return res;
- }
- - (BOOL)prefersHomeIndicatorAutoHidden
- {
- return UnityGetHideHomeButton();
- }
- @end
- @implementation UnityDefaultViewController
- // these will be updated in one place where we "sync" UI side orientation handling to unity side
- NSUInteger _supportedOrientations;
- - (id)init
- {
- if ((self = [super init]))
- {
- NSAssert(UnityShouldAutorotate(), @"UnityDefaultViewController should be used only if unity is set to autorotate");
- _supportedOrientations = EnabledAutorotationInterfaceOrientations();
- }
- return self;
- }
- - (void)updateSupportedOrientations
- {
- _supportedOrientations = EnabledAutorotationInterfaceOrientations();
- if (@available(iOS 16.0, *))
- [self setNeedsUpdateOfSupportedInterfaceOrientations];
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return _supportedOrientations;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- ScreenOrientation currentOrientation = UIViewControllerOrientation(self);
- [GetAppController() updateAppOrientation: ConvertToIosScreenOrientation(currentOrientation)];
- [super viewWillAppear: animated];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- ScreenOrientation currentOrientation = UIViewControllerOrientation(self);
- [GetAppController() updateAppOrientation: ConvertToIosScreenOrientation(currentOrientation)];
- [super viewDidAppear: animated];
- }
- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
- {
- // CODE ARCHEOLOGY: we were using UIViewControllerOrientation, but on showing view with "Requires full screen"
- // CODE ARCHEOLOGY: we will get the size/orientation *already* set, and the rotation logic would break
- const ScreenOrientation curOrient = GetAppController().unityView.contentOrientation;
- const ScreenOrientation newOrient = OrientationAfterTransform(curOrient, [coordinator targetTransform]);
- // in case of presentation controller it will take control over orientations
- // so to avoid crazy corner cases, make default view controller to ignore "wrong" orientations
- // as they will come only in case of presentation view controller and will be reverted anyway
- // NB: we still want to pass message to super, we just want to skip unity-specific magic
- NSUInteger targetMask = 1 << ConvertToIosScreenOrientation(newOrient);
- if (([self supportedInterfaceOrientations] & targetMask) != 0)
- {
- [UIView setAnimationsEnabled: UnityUseAnimatedAutorotation() ? YES : NO];
- [KeyboardDelegate StartReorientation];
- [GetAppController() interfaceWillChangeOrientationTo: ConvertToIosScreenOrientation(newOrient)];
- [coordinator animateAlongsideTransition: nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
- [self.view setNeedsLayout];
- [GetAppController() interfaceDidChangeOrientationFrom: ConvertToIosScreenOrientation(curOrient)];
- [KeyboardDelegate FinishReorientation];
- [UIView setAnimationsEnabled: YES];
- }];
- }
- [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
- }
- @end
- @interface UnityFixedOrientationViewController ()
- {
- UIInterfaceOrientation _fixedOrientation;
- }
- @end
- @implementation UnityFixedOrientationViewController
- - (instancetype)initWithOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- self = [super init];
- if (self)
- {
- _fixedOrientation = interfaceOrientation;
- }
- return self;
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return 1 << _fixedOrientation;
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
- {
- return _fixedOrientation;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [GetAppController() updateAppOrientation: _fixedOrientation];
- [super viewWillAppear: animated];
- }
- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
- {
- // Adding a call to notify about changed orientation. In iOS16 there was a runtime change where view controller
- // is not fully set up as soon as we make the view controller change in AppController at -transitionToViewController:.
- // And instead iOS calls this method, in other cases this method is not called. So we want to call
- // didTransitionToViewController here as this is the place where we get updated orientation.
- //
- // NB: Look for additional explanation at UnityAppController+ViewHandling.mm method -transitionToViewController: before
- // call to same method.
- [GetAppController() didTransitionToViewController: self fromViewController: self];
- [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
- }
- @end
- @implementation UnityPortraitOnlyViewController
- - (instancetype)init
- {
- self = [super initWithOrientation: UIInterfaceOrientationPortrait];
- return self;
- }
- @end
- @implementation UnityPortraitUpsideDownOnlyViewController
- - (instancetype)init
- {
- self = [super initWithOrientation: UIInterfaceOrientationPortraitUpsideDown];
- return self;
- }
- @end
- @implementation UnityLandscapeLeftOnlyViewController
- - (instancetype)init
- {
- self = [super initWithOrientation: UIInterfaceOrientationLandscapeLeft];
- return self;
- }
- @end
- @implementation UnityLandscapeRightOnlyViewController
- - (instancetype)init
- {
- self = [super initWithOrientation: UIInterfaceOrientationLandscapeRight];
- return self;
- }
- @end
- NSUInteger EnabledAutorotationInterfaceOrientations()
- {
- NSUInteger ret = 0;
- if (UnityIsOrientationEnabled(portrait))
- ret |= (1 << UIInterfaceOrientationPortrait);
- if (UnityDeviceSupportsUpsideDown() && UnityIsOrientationEnabled(portraitUpsideDown))
- ret |= (1 << UIInterfaceOrientationPortraitUpsideDown);
- if (UnityIsOrientationEnabled(landscapeLeft))
- ret |= (1 << UIInterfaceOrientationLandscapeRight);
- if (UnityIsOrientationEnabled(landscapeRight))
- ret |= (1 << UIInterfaceOrientationLandscapeLeft);
- // Handling unexpected case where autorotation is on and all the orientations are off by defaulting to current orientation.
- // Previously we returned 0 and iOS were handling it by keeping orientation as is. From iOS16 behaviour changed and the bug was raised.
- // Either way iOS requires us to provide non 0 value to supportedInterfaceOrientations.
- if (ret == 0)
- {
- NSLog(@"[Error] All orientations are off for autorotation. Preventing crash by using current orientation.");
- ret = (1 << [GetAppController() interfaceOrientation]);
- }
- return ret;
- }
- #endif // PLATFORM_IOS
|