UnityAppController.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #import "UnityAppController.h"
  2. #import "UnityAppController+ViewHandling.h"
  3. #import "UnityAppController+Rendering.h"
  4. #import "iPhone_Sensors.h"
  5. #import <CoreGraphics/CoreGraphics.h>
  6. #import <QuartzCore/QuartzCore.h>
  7. #import <QuartzCore/CADisplayLink.h>
  8. #import <Availability.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <OpenGLES/EAGL.h>
  11. #import <OpenGLES/EAGLDrawable.h>
  12. #import <OpenGLES/ES2/gl.h>
  13. #import <OpenGLES/ES2/glext.h>
  14. #include <mach/mach_time.h>
  15. // MSAA_DEFAULT_SAMPLE_COUNT was moved to iPhone_GlesSupport.h
  16. // ENABLE_INTERNAL_PROFILER and related defines were moved to iPhone_Profiler.h
  17. // kFPS define for removed: you can use Application.targetFrameRate (30 fps by default)
  18. // DisplayLink is the only run loop mode now - all others were removed
  19. #include "CrashReporter.h"
  20. #include "UI/OrientationSupport.h"
  21. #include "UI/UnityView.h"
  22. #include "UI/Keyboard.h"
  23. #include "UI/SplashScreen.h"
  24. #include "Unity/InternalProfiler.h"
  25. #include "Unity/DisplayManager.h"
  26. #include "Unity/EAGLContextHelper.h"
  27. #include "Unity/GlesHelper.h"
  28. #include "Unity/ObjCRuntime.h"
  29. #include "PluginBase/AppDelegateListener.h"
  30. #include <assert.h>
  31. #include <stdbool.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #include <sys/sysctl.h>
  35. #import "IOSPlatformSDK.h"
  36. // we assume that app delegate is never changed and we can cache it, instead of re-query UIApplication every time
  37. UnityAppController* _UnityAppController = nil;
  38. UnityAppController* GetAppController()
  39. {
  40. return _UnityAppController;
  41. }
  42. // we keep old bools around to support "old" code that might have used them
  43. bool _ios81orNewer = false, _ios82orNewer = false, _ios83orNewer = false, _ios90orNewer = false, _ios91orNewer = false;
  44. bool _ios100orNewer = false, _ios101orNewer = false, _ios102orNewer = false, _ios103orNewer = false;
  45. bool _ios110orNewer = false, _ios111orNewer = false, _ios112orNewer = false;
  46. bool _ios130orNewer = false;
  47. // was unity rendering already inited: we should not touch rendering while this is false
  48. bool _renderingInited = false;
  49. // was unity inited: we should not touch unity api while this is false
  50. bool _unityAppReady = false;
  51. // see if there's a need to do internal player pause/resume handling
  52. //
  53. // Typically the trampoline code should manage this internally, but
  54. // there are use cases, videoplayer, plugin code, etc where the player
  55. // is paused before the internal handling comes relevant. Avoid
  56. // overriding externally managed player pause/resume handling by
  57. // caching the state
  58. bool _wasPausedExternal = false;
  59. // should we skip present on next draw: used in corner cases (like rotation) to fill both draw-buffers with some content
  60. bool _skipPresent = false;
  61. // was app "resigned active": some operations do not make sense while app is in background
  62. bool _didResignActive = false;
  63. // was startUnity scheduled: used to make startup robust in case of locking device
  64. static bool _startUnityScheduled = false;
  65. bool _supportsMSAA = false;
  66. #if UNITY_SUPPORT_ROTATION
  67. // Required to enable specific orientation for some presentation controllers: see supportedInterfaceOrientationsForWindow below for details
  68. NSInteger _forceInterfaceOrientationMask = 0;
  69. #endif
  70. @implementation UnityAppController
  71. @synthesize unityView = _unityView;
  72. @synthesize unityDisplayLink = _displayLink;
  73. @synthesize rootView = _rootView;
  74. @synthesize rootViewController = _rootController;
  75. @synthesize mainDisplay = _mainDisplay;
  76. @synthesize renderDelegate = _renderDelegate;
  77. @synthesize quitHandler = _quitHandler;
  78. #if UNITY_SUPPORT_ROTATION
  79. @synthesize interfaceOrientation = _curOrientation;
  80. #endif
  81. - (id)init
  82. {
  83. if ((self = _UnityAppController = [super init]))
  84. {
  85. // due to clang issues with generating warning for overriding deprecated methods
  86. // we will simply assert if deprecated methods are present
  87. // NB: methods table is initied at load (before this call), so it is ok to check for override
  88. NSAssert(![self respondsToSelector: @selector(createUnityViewImpl)],
  89. @"createUnityViewImpl is deprecated and will not be called. Override createUnityView"
  90. );
  91. NSAssert(![self respondsToSelector: @selector(createViewHierarchyImpl)],
  92. @"createViewHierarchyImpl is deprecated and will not be called. Override willStartWithViewController"
  93. );
  94. NSAssert(![self respondsToSelector: @selector(createViewHierarchy)],
  95. @"createViewHierarchy is deprecated and will not be implemented. Use createUI"
  96. );
  97. }
  98. return self;
  99. }
  100. - (void)setWindow:(id)object {}
  101. - (UIWindow*)window { return _window; }
  102. - (void)shouldAttachRenderDelegate {}
  103. - (void)preStartUnity {}
  104. - (void)startUnity:(UIApplication*)application
  105. {
  106. NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");
  107. UnityInitApplicationGraphics();
  108. // we make sure that first level gets correct display list and orientation
  109. [[DisplayManager Instance] updateDisplayListCacheInUnity];
  110. UnityLoadApplication();
  111. Profiler_InitProfiler();
  112. [self showGameUI];
  113. [self createDisplayLink];
  114. UnitySetPlayerFocus(1);
  115. AVAudioSession* audioSession = [AVAudioSession sharedInstance];
  116. [audioSession setActive: YES error: nil];
  117. [audioSession addObserver: self forKeyPath: @"outputVolume" options: 0 context: nil];
  118. UnityUpdateMuteState([audioSession outputVolume] < 0.01f ? 1 : 0);
  119. }
  120. extern "C" void UnityDestroyDisplayLink()
  121. {
  122. [GetAppController() destroyDisplayLink];
  123. }
  124. extern "C" void UnityRequestQuit()
  125. {
  126. _didResignActive = true;
  127. if (GetAppController().quitHandler)
  128. GetAppController().quitHandler();
  129. else
  130. exit(0);
  131. }
  132. extern void SensorsCleanup();
  133. extern "C" void UnityCleanupTrampoline()
  134. {
  135. // Unity view and viewController will not necessary be destroyed right after this function execution.
  136. // We need to ensure that these objects will not receive any callbacks from system during that time.
  137. [_UnityAppController window].rootViewController = nil;
  138. [[_UnityAppController unityView] removeFromSuperview];
  139. // Prevent multiple cleanups
  140. if (_UnityAppController == nil)
  141. return;
  142. [KeyboardDelegate Destroy];
  143. SensorsCleanup();
  144. Profiler_UninitProfiler();
  145. [DisplayManager Destroy];
  146. UnityDestroyDisplayLink();
  147. _UnityAppController = nil;
  148. }
  149. #if UNITY_SUPPORT_ROTATION
  150. - (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
  151. {
  152. // No rootViewController is set because we are switching from one view controller to another, all orientations should be enabled
  153. if ([window rootViewController] == nil)
  154. return UIInterfaceOrientationMaskAll;
  155. // Some presentation controllers (e.g. UIImagePickerController) require portrait orientation and will throw exception if it is not supported.
  156. // At the same time enabling all orientations by returning UIInterfaceOrientationMaskAll might cause unwanted orientation change
  157. // (e.g. when using UIActivityViewController to "share to" another application, iOS will use supportedInterfaceOrientations to possibly reorient).
  158. // So to avoid exception we are returning combination of constraints for root view controller and orientation requested by iOS.
  159. // _forceInterfaceOrientationMask is updated in willChangeStatusBarOrientation, which is called if some presentation controller insists on orientation change.
  160. return [[window rootViewController] supportedInterfaceOrientations] | _forceInterfaceOrientationMask;
  161. }
  162. - (void)application:(UIApplication*)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration
  163. {
  164. // Setting orientation mask which is requested by iOS: see supportedInterfaceOrientationsForWindow above for details
  165. _forceInterfaceOrientationMask = 1 << newStatusBarOrientation;
  166. }
  167. #endif
  168. #if !PLATFORM_TVOS
  169. - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
  170. {
  171. AppController_SendNotificationWithArg(kUnityDidReceiveLocalNotification, notification);
  172. UnitySendLocalNotification(notification);
  173. }
  174. #endif
  175. #if UNITY_USES_REMOTE_NOTIFICATIONS
  176. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
  177. {
  178. AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo);
  179. UnitySendRemoteNotification(userInfo);
  180. }
  181. - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
  182. {
  183. AppController_SendNotificationWithArg(kUnityDidRegisterForRemoteNotificationsWithDeviceToken, deviceToken);
  184. UnitySendDeviceToken(deviceToken);
  185. }
  186. #if !PLATFORM_TVOS
  187. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
  188. {
  189. AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo);
  190. UnitySendRemoteNotification(userInfo);
  191. if (handler)
  192. {
  193. handler(UIBackgroundFetchResultNoData);
  194. }
  195. }
  196. #endif
  197. - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
  198. {
  199. AppController_SendNotificationWithArg(kUnityDidFailToRegisterForRemoteNotificationsWithError, error);
  200. UnitySendRemoteNotificationError(error);
  201. // alas people do not check remote notification error through api (which is clunky, i agree) so log here to have at least some visibility
  202. ::printf("\nFailed to register for remote notifications:\n%s\n\n", [[error localizedDescription] UTF8String]);
  203. }
  204. #endif
  205. // UIApplicationOpenURLOptionsKey was added only in ios10 sdk, while we still support ios9 sdk
  206. - (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<NSString*, id>*)options
  207. {
  208. id sourceApplication = options[UIApplicationOpenURLOptionsSourceApplicationKey], annotation = options[UIApplicationOpenURLOptionsAnnotationKey];
  209. NSMutableDictionary<NSString*, id>* notifData = [NSMutableDictionary dictionaryWithCapacity: 3];
  210. if (url)
  211. {
  212. notifData[@"url"] = url;
  213. UnitySetAbsoluteURL(url.absoluteString.UTF8String);
  214. }
  215. if (sourceApplication) notifData[@"sourceApplication"] = sourceApplication;
  216. if (annotation) notifData[@"annotation"] = annotation;
  217. AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);
  218. IOSPlatformSDK * sdk = [IOSPlatformSDK sharedInstance];
  219. [sdk startWithUrl:url];
  220. return YES;
  221. }
  222. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
  223. #if defined(__IPHONE_12_0) || defined(__TVOS_12_0)
  224. restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring> > * _Nullable restorableObjects))restorationHandler
  225. #else
  226. restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
  227. #endif
  228. {
  229. NSURL* url = userActivity.webpageURL;
  230. if (url)
  231. UnitySetAbsoluteURL(url.absoluteString.UTF8String);
  232. return YES;
  233. }
  234. - (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  235. {
  236. AppController_SendNotificationWithArg(kUnityWillFinishLaunchingWithOptions, launchOptions);
  237. return YES;
  238. }
  239. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  240. {
  241. ::printf("-> applicationDidFinishLaunching()\n");
  242. // send notfications
  243. #if !PLATFORM_TVOS
  244. if (UILocalNotification* notification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey])
  245. UnitySendLocalNotification(notification);
  246. if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
  247. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  248. #endif
  249. UnityInitApplicationNoGraphics(UnityDataBundleDir());
  250. [self selectRenderingAPI];
  251. [UnityRenderingView InitializeForAPI: self.renderingAPI];
  252. _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
  253. _unityView = [self createUnityView];
  254. [DisplayManager Initialize];
  255. _mainDisplay = [DisplayManager Instance].mainDisplay;
  256. [_mainDisplay createWithWindow: _window andView: _unityView];
  257. [self createUI];
  258. [self preStartUnity];
  259. // if you wont use keyboard you may comment it out at save some memory
  260. [KeyboardDelegate Initialize];
  261. //以下是几种打开App方式
  262. if(launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
  263. NSLog(@"打开方式 远程推送打开");
  264. }else if(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]){
  265. NSLog(@"打开方式 本地推送打开");
  266. }else if(launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]){
  267. NSLog(@"打开方式 UniversalLinks打开");
  268. }else if(launchOptions[UIApplicationLaunchOptionsURLKey]){
  269. //我们需要在此处处理通过 scheme 打开App并截获参数。
  270. NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
  271. // NSLog(@"打开方式 通过URL打开的 ===== >> %@",url);
  272. // IOSPlatformSDK * sdk = [IOSPlatformSDK sharedInstance];
  273. // [sdk startWithUrl:url];
  274. }else if (!launchOptions){
  275. NSLog(@"打开方式 手动点击打开");
  276. [IOSPlatformSDK sharedInstance];
  277. }
  278. //36a0c05291
  279. return YES;
  280. }
  281. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context
  282. {
  283. if ([keyPath isEqual: @"outputVolume"])
  284. {
  285. UnityUpdateMuteState([[AVAudioSession sharedInstance] outputVolume] < 0.01f ? 1 : 0);
  286. }
  287. }
  288. -(void)applicationDidEnterBackground:(UIApplication*)application{
  289. ::printf("-> applicationDidEnterBackground()\n");
  290. BTDataProcess * sharedInstance = [BTDataProcess sharedInstance];
  291. sharedInstance.isBackGround = YES;
  292. //开启后台任务保活
  293. // [self comeToBackgroundMode];
  294. }
  295. //#pragma mark -- leon add
  296. //-(void)comeToBackgroundMode{
  297. // //初始化一个后台任务BackgroundTask,这个后台任务的作用就是告诉系统当前app在后台有任务处理,需要时间
  298. // UIApplication* app = [UIApplication sharedApplication];
  299. // self.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
  300. // [app endBackgroundTask:self.bgTask];
  301. // self.bgTask = UIBackgroundTaskInvalid;
  302. // }];
  303. // //开启定时器 不断向系统请求后台任务执行的时间
  304. // NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(applyForMoreTime) userInfo:nil repeats:YES];
  305. // [timer fire];
  306. //}
  307. //
  308. //-(void)applyForMoreTime{
  309. // //如果系统给的剩余时间小于60秒 就终止当前的后台任务,再重新初始化一个后台任务,重新让系统分配时间,这样一直循环下去,保持APP在后台一直处于active状态。
  310. // if ([UIApplication sharedApplication].backgroundTimeRemaining < 60){
  311. // [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
  312. // self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  313. // [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
  314. // self.bgTask = UIBackgroundTaskInvalid;
  315. // }];
  316. // }
  317. //}
  318. - (void)applicationWillEnterForeground:(UIApplication*)application
  319. {
  320. ::printf("-> applicationWillEnterForeground()\n");
  321. BTDataProcess * sharedInstance = [BTDataProcess sharedInstance];
  322. sharedInstance.isBackGround = NO;
  323. [sharedInstance applicationWillEnterForeground];
  324. // applicationWillEnterForeground: might sometimes arrive *before* actually initing unity (e.g. locking on startup)
  325. if (_unityAppReady)
  326. {
  327. // if we were showing video before going to background - the view size may be changed while we are in background
  328. [GetAppController().unityView recreateRenderingSurfaceIfNeeded];
  329. }
  330. }
  331. - (void)applicationDidBecomeActive:(UIApplication*)application
  332. {
  333. ::printf("-> applicationDidBecomeActive()\n");
  334. [self removeSnapshotView];
  335. if (_unityAppReady)
  336. {
  337. if (UnityIsPaused() && _wasPausedExternal == false)
  338. {
  339. UnityWillResume();
  340. UnityPause(0);
  341. }
  342. if (_wasPausedExternal)
  343. {
  344. if (UnityIsFullScreenPlaying())
  345. TryResumeFullScreenVideo();
  346. }
  347. // need to do this with delay because FMOD restarts audio in AVAudioSessionInterruptionNotification handler
  348. [self performSelector: @selector(updateUnityAudioOutput) withObject: nil afterDelay: 0.1];
  349. UnitySetPlayerFocus(1);
  350. }
  351. else if (!_startUnityScheduled)
  352. {
  353. _startUnityScheduled = true;
  354. [self performSelector: @selector(startUnity:) withObject: application afterDelay: 0];
  355. }
  356. _didResignActive = false;
  357. }
  358. - (void)updateUnityAudioOutput
  359. {
  360. UnityUpdateAudioOutputState();
  361. UnityUpdateMuteState([[AVAudioSession sharedInstance] outputVolume] < 0.01f ? 1 : 0);
  362. }
  363. - (void)removeSnapshotView
  364. {
  365. // do this on the main queue async so that if we try to create one
  366. // and remove in the same frame, this always happens after in the same queue
  367. dispatch_async(dispatch_get_main_queue(), ^{
  368. if (_snapshotView)
  369. {
  370. [_snapshotView removeFromSuperview];
  371. _snapshotView = nil;
  372. // Make sure that the keyboard input field regains focus after the application becomes active.
  373. [[KeyboardDelegate Instance] becomeFirstResponder];
  374. }
  375. });
  376. }
  377. - (void)applicationWillResignActive:(UIApplication*)application
  378. {
  379. ::printf("-> applicationWillResignActive()\n");
  380. if (_unityAppReady)
  381. {
  382. UnitySetPlayerFocus(0);
  383. _wasPausedExternal = UnityIsPaused();
  384. if (_wasPausedExternal == false)
  385. {
  386. // Pause Unity only if we don't need special background processing
  387. // otherwise batched player loop can be called to run user scripts.
  388. if (!UnityGetUseCustomAppBackgroundBehavior())
  389. {
  390. // Force player to do one more frame, so scripts get a chance to render custom screen for minimized app in task manager.
  391. // NB: UnityWillPause will schedule OnApplicationPause message, which will be sent normally inside repaint (unity player loop)
  392. // NB: We will actually pause after the loop (when calling UnityPause).
  393. UnityWillPause();
  394. [self repaint];
  395. UnityPause(1);
  396. // this is done on the next frame so that
  397. // in the case where unity is paused while going
  398. // into the background and an input is deactivated
  399. // we don't mess with the view hierarchy while taking
  400. // a view snapshot (case 760747).
  401. dispatch_async(dispatch_get_main_queue(), ^{
  402. // if we are active again, we don't need to do this anymore
  403. if (!_didResignActive)
  404. {
  405. return;
  406. }
  407. _snapshotView = [self createSnapshotView];
  408. if (_snapshotView)
  409. [_rootView addSubview: _snapshotView];
  410. });
  411. }
  412. }
  413. }
  414. _didResignActive = true;
  415. }
  416. - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application
  417. {
  418. ::printf("WARNING -> applicationDidReceiveMemoryWarning()\n");
  419. UnityLowMemory();
  420. }
  421. - (void)applicationWillTerminate:(UIApplication*)application
  422. {
  423. ::printf("-> applicationWillTerminate()\n");
  424. // Only clean up if Unity has finished initializing, else the clean up process will crash,
  425. // this happens if the app is force closed immediately after opening it.
  426. if (_unityAppReady)
  427. {
  428. UnityCleanup();
  429. UnityCleanupTrampoline();
  430. }
  431. }
  432. - (void)application:(UIApplication*)application handleEventsForBackgroundURLSession:(nonnull NSString *)identifier completionHandler:(nonnull void (^)())completionHandler
  433. {
  434. NSDictionary* arg = @{identifier: completionHandler};
  435. AppController_SendNotificationWithArg(kUnityHandleEventsForBackgroundURLSession, arg);
  436. }
  437. @end
  438. void AppController_SendNotification(NSString* name)
  439. {
  440. [[NSNotificationCenter defaultCenter] postNotificationName: name object: GetAppController()];
  441. }
  442. void AppController_SendNotificationWithArg(NSString* name, id arg)
  443. {
  444. [[NSNotificationCenter defaultCenter] postNotificationName: name object: GetAppController() userInfo: arg];
  445. }
  446. void AppController_SendUnityViewControllerNotification(NSString* name)
  447. {
  448. [[NSNotificationCenter defaultCenter] postNotificationName: name object: UnityGetGLViewController()];
  449. }
  450. extern "C" UIWindow* UnityGetMainWindow()
  451. {
  452. return GetAppController().mainDisplay.window;
  453. }
  454. extern "C" UIViewController* UnityGetGLViewController()
  455. {
  456. return GetAppController().rootViewController;
  457. }
  458. extern "C" UIView* UnityGetGLView()
  459. {
  460. return GetAppController().unityView;
  461. }
  462. extern "C" ScreenOrientation UnityCurrentOrientation() { return GetAppController().unityView.contentOrientation; }
  463. bool LogToNSLogHandler(LogType logType, const char* log, va_list list)
  464. {
  465. NSLogv([NSString stringWithUTF8String: log], list);
  466. return true;
  467. }
  468. static void AddNewAPIImplIfNeeded();
  469. // From https://stackoverflow.com/questions/4744826/detecting-if-ios-app-is-run-in-debugger
  470. static bool isDebuggerAttachedToConsole(void)
  471. // Returns true if the current process is being debugged (either
  472. // running under the debugger or has a debugger attached post facto).
  473. {
  474. int junk;
  475. int mib[4];
  476. struct kinfo_proc info;
  477. size_t size;
  478. // Initialize the flags so that, if sysctl fails for some bizarre
  479. // reason, we get a predictable result.
  480. info.kp_proc.p_flag = 0;
  481. // Initialize mib, which tells sysctl the info we want, in this case
  482. // we're looking for information about a specific process ID.
  483. mib[0] = CTL_KERN;
  484. mib[1] = KERN_PROC;
  485. mib[2] = KERN_PROC_PID;
  486. mib[3] = getpid();
  487. // Call sysctl.
  488. size = sizeof(info);
  489. junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
  490. assert(junk == 0);
  491. // We're being debugged if the P_TRACED flag is set.
  492. return ((info.kp_proc.p_flag & P_TRACED) != 0);
  493. }
  494. void UnityInitTrampoline()
  495. {
  496. InitCrashHandling();
  497. NSString* version = [[UIDevice currentDevice] systemVersion];
  498. #define CHECK_VER(s) [version compare: s options: NSNumericSearch] != NSOrderedAscending
  499. _ios81orNewer = CHECK_VER(@"8.1"), _ios82orNewer = CHECK_VER(@"8.2"), _ios83orNewer = CHECK_VER(@"8.3");
  500. _ios90orNewer = CHECK_VER(@"9.0"), _ios91orNewer = CHECK_VER(@"9.1");
  501. _ios100orNewer = CHECK_VER(@"10.0"), _ios101orNewer = CHECK_VER(@"10.1"), _ios102orNewer = CHECK_VER(@"10.2"), _ios103orNewer = CHECK_VER(@"10.3");
  502. _ios110orNewer = CHECK_VER(@"11.0"), _ios111orNewer = CHECK_VER(@"11.1"), _ios112orNewer = CHECK_VER(@"11.2");
  503. _ios130orNewer = CHECK_VER(@"13.0");
  504. #undef CHECK_VER
  505. AddNewAPIImplIfNeeded();
  506. #if !TARGET_IPHONE_SIMULATOR
  507. // Use NSLog logging if a debugger is not attached, otherwise we write to stdout.
  508. if (!isDebuggerAttachedToConsole())
  509. UnitySetLogEntryHandler(LogToNSLogHandler);
  510. #endif
  511. }
  512. extern "C" bool UnityiOS81orNewer() { return _ios81orNewer; }
  513. extern "C" bool UnityiOS82orNewer() { return _ios82orNewer; }
  514. extern "C" bool UnityiOS90orNewer() { return _ios90orNewer; }
  515. extern "C" bool UnityiOS91orNewer() { return _ios91orNewer; }
  516. extern "C" bool UnityiOS100orNewer() { return _ios100orNewer; }
  517. extern "C" bool UnityiOS101orNewer() { return _ios101orNewer; }
  518. extern "C" bool UnityiOS102orNewer() { return _ios102orNewer; }
  519. extern "C" bool UnityiOS103orNewer() { return _ios103orNewer; }
  520. extern "C" bool UnityiOS110orNewer() { return _ios110orNewer; }
  521. extern "C" bool UnityiOS111orNewer() { return _ios111orNewer; }
  522. extern "C" bool UnityiOS112orNewer() { return _ios112orNewer; }
  523. extern "C" bool UnityiOS130orNewer() { return _ios130orNewer; }
  524. // sometimes apple adds new api with obvious fallback on older ios.
  525. // in that case we simply add these functions ourselves to simplify code
  526. static void AddNewAPIImplIfNeeded()
  527. {
  528. if (![[UIScreen class] instancesRespondToSelector: @selector(maximumFramesPerSecond)])
  529. {
  530. IMP UIScreen_MaximumFramesPerSecond_IMP = imp_implementationWithBlock(^NSInteger(id _self) {
  531. return 60;
  532. });
  533. class_replaceMethod([UIScreen class], @selector(maximumFramesPerSecond), UIScreen_MaximumFramesPerSecond_IMP, UIScreen_maximumFramesPerSecond_Enc);
  534. }
  535. if (![[UIView class] instancesRespondToSelector: @selector(safeAreaInsets)])
  536. {
  537. IMP UIView_SafeAreaInsets_IMP = imp_implementationWithBlock(^UIEdgeInsets(id _self) {
  538. return UIEdgeInsetsZero;
  539. });
  540. class_replaceMethod([UIView class], @selector(safeAreaInsets), UIView_SafeAreaInsets_IMP, UIView_safeAreaInsets_Enc);
  541. }
  542. }