Last active
October 17, 2016 15:12
-
-
Save aet/c13c0ca30f980a6cb090 to your computer and use it in GitHub Desktop.
Workaround for Unity fullscreen videoplayer missing "done" button when using AVKit implementation on iOS 8 and later
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Classes/UI/UnityAppController+ViewHandling.h b/Classes/UI/UnityAppController+ViewHandling.h | |
--- a/Classes/UI/UnityAppController+ViewHandling.h | |
+++ b/Classes/UI/UnityAppController+ViewHandling.h | |
@@ -72,10 +72,13 @@ | |
// creates initial UI hierarchy (e.g. splash screen) and calls willStartWithViewController | |
- (void)createUI; | |
// shows game itself (hides splash, and bring _rootView to front) | |
- (void)showGameUI; | |
+// returns the topmost presentedViewController if there is one, or just rootViewController | |
+- (UIViewController*)topMostController; | |
+ | |
// will create or return from cache correct view controller for requested orientation | |
- (UIViewController*)createRootViewController; | |
#if !UNITY_TVOS | |
// will create or return from cache correct view controller for given orientation | |
- (UIViewController*)createRootViewControllerForOrientation:(UIInterfaceOrientation)orientation; | |
diff --git a/Classes/UI/UnityAppController+ViewHandling.mm b/Classes/UI/UnityAppController+ViewHandling.mm | |
--- a/Classes/UI/UnityAppController+ViewHandling.mm | |
+++ b/Classes/UI/UnityAppController+ViewHandling.mm | |
@@ -68,10 +68,20 @@ | |
return _viewControllerForOrientation[orientation]; | |
} | |
#endif | |
+- (UIViewController*)topMostController | |
+{ | |
+ UIViewController *topController = self.window.rootViewController; | |
+ while (topController.presentedViewController) | |
+ { | |
+ topController = topController.presentedViewController; | |
+ } | |
+ | |
+ return topController; | |
+} | |
- (UIViewController*)createRootViewController | |
{ | |
#if UNITY_TVOS | |
return [self createUnityViewControllerForTVOS]; | |
#else | |
diff --git a/Classes/Unity/FullScreenVideoPlayer.mm b/Classes/Unity/FullScreenVideoPlayer.mm | |
--- a/Classes/Unity/FullScreenVideoPlayer.mm | |
+++ b/Classes/Unity/FullScreenVideoPlayer.mm | |
@@ -5,10 +5,11 @@ | |
#include "UnityAppController.h" | |
#include "UI/UnityView.h" | |
#include "UI/UnityViewControllerBase.h" | |
#include "UI/OrientationSupport.h" | |
+#include "UI/UnityAppController+ViewHandling.h" | |
#include "Unity/ObjCRuntime.h" | |
#include "Unity/VideoPlayer.h" | |
#if !UNITY_TVOS | |
@@ -36,11 +37,11 @@ | |
- (void)actuallyStartTheMovie:(NSURL*)url; | |
- (void)moviePlayBackDidFinish:(NSNotification*)notification; | |
- (void)finish; | |
@end | |
-@interface AVKitVideoPlayback : NSObject<VideoPlayerDelegate> | |
+@interface AVKitVideoPlayback : NSObject<VideoPlayerDelegate,UIViewControllerTransitioningDelegate> | |
{ | |
AVPlayerViewController* videoViewController; | |
VideoPlayer* videoPlayer; | |
CancelOnTouchView* cancelOnTouchView; | |
@@ -197,10 +198,11 @@ | |
{ | |
videoViewController = [[_AVPlayerViewControllerClass alloc] init]; | |
videoViewController.showsPlaybackControls = showControls; | |
videoViewController.view.backgroundColor = bgColor; | |
+ videoViewController.transitioningDelegate = self; | |
if(cancelOnTouch) | |
{ | |
cancelOnTouchView = [[CancelOnTouchView alloc] initWithOnTouchBlock:^(){ | |
if(_AVKitVideoPlayback) | |
@@ -216,23 +218,39 @@ | |
} | |
- (void)onPlayerReady | |
{ | |
videoViewController.player = videoPlayer.player; | |
[videoPlayer playVideoPlayer]; | |
- GetAppController().window.rootViewController = videoViewController; | |
+ UIViewController *viewController = [GetAppController() topMostController]; | |
+ if ([viewController isEqual:videoViewController] == NO && [videoViewController isBeingPresented] == NO) | |
+ { | |
+ [viewController presentViewController:videoViewController animated:NO completion:nil]; | |
+ } | |
} | |
- (void)onPlayerDidFinishPlayingVideo | |
{ | |
[self finish]; | |
} | |
- (void)onPlayerError:(NSError*)error | |
{ | |
[self finish]; | |
} | |
+- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed { | |
+ if ([dismissed isEqual:videoViewController] == YES) | |
+ { | |
+ [self finish]; | |
+ } | |
+ | |
+ return nil; | |
+} | |
- (void)finish | |
{ | |
- GetAppController().window.rootViewController = GetAppController().rootViewController; | |
+ UIViewController *viewController = [GetAppController() topMostController]; | |
+ if ([viewController isEqual:videoViewController] == YES && [viewController isBeingDismissed] == NO) | |
+ { | |
+ [viewController dismissViewControllerAnimated:NO completion:nil]; | |
+ } | |
[cancelOnTouchView removeFromSuperview]; | |
[videoPlayer unloadPlayer]; | |
cancelOnTouchView = nil; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this!
Works very well.
I would like to add that if you want to loop the video, adjust the function -(void)onPlayerDidFinishPlayingVideo in FullScreenVideoPlayer.mm as such: