Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "UnityiOSForcedUpdater.h"
- #import "UnityAppController.h"
- extern bool _unityAppReady;
- @implementation UnityiOSForcedUpdater
- extern "C"
- {
- void _disableForcedUpdateLoop()
- {
- [UnityiOSForcedUpdater stopBackgroundUpdateTimer];
- }
- }
- static NSTimer* updateTimer;
- static NSTimer* timeoutTimer;
- static UIBackgroundTaskIdentifier backgroundTask;
- static UIApplication* app;
- static bool runLoopIsEnabled;
- static bool stopRequested;
- + (void) startBackgroundUpdateTimer
- {
- if ((updateTimer == nil) && (stopRequested == false))
- {
- app = [UIApplication sharedApplication];
- backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{[self stopBackgroundUpdateTimer];}];
- updateTimer = [NSTimer timerWithTimeInterval: 0.25 target: self selector: @selector(updateUnityLoop) userInfo: nil repeats: YES];
- timeoutTimer = [NSTimer timerWithTimeInterval: 10 target: self selector: @selector(stopBackgroundUpdateTimer) userInfo: nil repeats: NO];
- NSRunLoop* runloop = [NSRunLoop currentRunLoop];
- [runloop addTimer:updateTimer forMode:NSDefaultRunLoopMode];
- [runloop addTimer:timeoutTimer forMode:NSDefaultRunLoopMode];
- runLoopIsEnabled = true;
- NSDate* date = [NSDate distantPast];
- while(runLoopIsEnabled && [runloop runMode:NSDefaultRunLoopMode beforeDate:date]);
- }
- else
- {
- stopRequested = false;
- }
- }
- + (void) stopBackgroundUpdateTimer
- {
- if (updateTimer != nil)
- {
- printf(" * stop please * ");
- [updateTimer invalidate];
- updateTimer = nil;
- [timeoutTimer invalidate];
- timeoutTimer = nil;
- [app endBackgroundTask:backgroundTask];
- runLoopIsEnabled = false;
- stopRequested = false;
- }
- else
- {
- printf(" * stop requested * ");
- stopRequested = true;
- }
- }
- + (void) updateUnityLoop
- {
- if (_unityAppReady)
- {
- printf(" - tick - ");
- UnityBatchPlayerLoop();
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement