Advertisement
bjoWasTaken

UnityiOSForcedUpdater.mm

Aug 5th, 2016
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "UnityiOSForcedUpdater.h"
  2. #import "UnityAppController.h"
  3.  
  4. extern bool _unityAppReady;
  5.  
  6. @implementation UnityiOSForcedUpdater
  7.  
  8. extern "C"
  9. {
  10.     void _disableForcedUpdateLoop()
  11.     {
  12.         [UnityiOSForcedUpdater stopBackgroundUpdateTimer];
  13.     }
  14. }
  15.  
  16. static NSTimer* updateTimer;
  17. static NSTimer* timeoutTimer;
  18. static UIBackgroundTaskIdentifier backgroundTask;
  19. static UIApplication* app;
  20. static bool runLoopIsEnabled;
  21. static bool stopRequested;
  22.  
  23. + (void) startBackgroundUpdateTimer
  24. {
  25.     if ((updateTimer == nil) && (stopRequested == false))
  26.     {
  27.         app = [UIApplication sharedApplication];
  28.         backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{[self stopBackgroundUpdateTimer];}];
  29.         updateTimer = [NSTimer timerWithTimeInterval: 0.25 target: self selector: @selector(updateUnityLoop) userInfo: nil repeats: YES];
  30.         timeoutTimer = [NSTimer timerWithTimeInterval: 10 target: self selector: @selector(stopBackgroundUpdateTimer) userInfo: nil repeats: NO];
  31.        
  32.         NSRunLoop* runloop = [NSRunLoop currentRunLoop];
  33.        
  34.         [runloop addTimer:updateTimer forMode:NSDefaultRunLoopMode];
  35.         [runloop addTimer:timeoutTimer forMode:NSDefaultRunLoopMode];
  36.        
  37.         runLoopIsEnabled = true;
  38.         NSDate* date = [NSDate distantPast];
  39.        
  40.         while(runLoopIsEnabled && [runloop runMode:NSDefaultRunLoopMode beforeDate:date]);
  41.     }
  42.     else
  43.     {
  44.         stopRequested = false;
  45.     }
  46. }
  47.  
  48. + (void) stopBackgroundUpdateTimer
  49. {
  50.     if (updateTimer != nil)
  51.     {
  52.         printf(" * stop please * ");
  53.         [updateTimer invalidate];
  54.         updateTimer = nil;
  55.        
  56.         [timeoutTimer invalidate];
  57.         timeoutTimer = nil;
  58.        
  59.         [app endBackgroundTask:backgroundTask];
  60.         runLoopIsEnabled = false;
  61.        
  62.         stopRequested = false;
  63.     }
  64.     else
  65.     {
  66.         printf(" * stop requested * ");
  67.         stopRequested = true;
  68.     }
  69. }
  70.  
  71. + (void) updateUnityLoop
  72. {
  73.     if (_unityAppReady)
  74.     {
  75.         printf(" - tick - ");
  76.         UnityBatchPlayerLoop();
  77.     }
  78. }
  79.  
  80. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement