赞
踩
因为项目从5.x升级到2018版,发现以前发的教程无效了,只适用5.x,所以重新写了个方法。
第一步找到unity 项目\Assets\Plugins\iOS文件夹,没有文件夹则创建一个。
在文件夹内创建oc类:
创建头文件AddViewSdk.h
#import <Foundation/Foundation.h>
@interface AddViewSdk:NSObject
+(AddViewSdk *) GetInstance;
-(void) showSplash;
-(void) hiSplash;
@end
创建AddViewSdk.m文件:
// Unity-iPhone // // Created by kibou on 2020/9/19. // #import "AddViewSdk.h" #import <AVKit/AVKit.h> #import "DeviceListCollection.h" #import "WaitViewController.h" static AddViewSdk* instance; @interface AddViewSdk() @property (atomic, retain) AVPlayerViewController *AV_vc; @property (atomic, retain) UIViewController *Image_vc; @end @implementation AddViewSdk +(AddViewSdk *)GetInstance{ if (instance==nil) { instance = [[AddViewSdk alloc] init]; } return instance; } -(void) showSplash{ CGRect rect = [[UIScreen mainScreen] bounds]; _Image_vc= [[UIViewController alloc] init]; UIImageView *bg = [[UIImageView alloc] initWithFrame:rect]; NSString *path = [[NSBundle mainBundle] pathForResource:@"logovideo0" ofType:@"png"]; UIImage *image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path]]]; [bg setImage:image2]; [_Image_vc.view addSubview:bg]; _Image_vc.view.frame = rect; [UnityGetGLViewController() addChildViewController:_Image_vc]; [UnityGetGLViewController().view addSubview:_Image_vc.view]; NSString *path2 = [[NSBundle mainBundle] pathForResource:@"logovideo" ofType:@"mp4"]; //为即将播放的视频内容进行建模 AVPlayerItem *avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path2]]; //创建监听(这是一种KOV的监听模式) // [avplayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:avplayerItem]; //给播放器赋值要播放的对象模型 AVPlayer *avplayer = [AVPlayer playerWithPlayerItem:avplayerItem]; //指定显示的Layer AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avplayer]; layer.frame = rect; layer.videoGravity = AVLayerVideoGravityResizeAspectFill; _AV_vc = [[AVPlayerViewController alloc] init]; _AV_vc.showsPlaybackControls = NO; _AV_vc.videoGravity = AVLayerVideoGravityResizeAspectFill; _AV_vc.player = avplayer; _AV_vc.view.frame = rect; [_AV_vc.player play]; [UnityGetGLViewController() addChildViewController:_AV_vc]; // [UnityGetGLViewController().view addSubview:_AV_vc.view]; [UnityGetGLViewController().view insertSubview:_AV_vc.view atIndex:[UnityGetGLViewController().view.subviews count]-1]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(showAVLayer) userInfo:nil repeats:NO]; } -(void) hiSplash{ [_AV_vc.view removeFromSuperview]; //2 [_AV_vc removeFromParentViewController]; //3 _AV_vc =nil; } -(void) showAVLayer{ [_Image_vc.view removeFromSuperview]; //2 [_Image_vc removeFromParentViewController]; //3 _Image_vc =nil; } - (void)moviePlayDidEnd:(NSNotification *)notification{ NSLog(@"播放完毕"); [self hiSplash]; //视频播放完毕操作 } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ AVPlayerItem *item = object; //判断监听对象的状态 if ([keyPath isEqualToString:@"status"]) { if (item.status == AVPlayerItemStatusReadyToPlay) {//准备好的 NSLog(@"AVPlayerItemStatusReadyToPlay"); } else if(item.status ==AVPlayerItemStatusUnknown){//未知的状态 NSLog(@"AVPlayerItemStatusUnknown"); }else if(item.status ==AVPlayerItemStatusFailed){//有错误的 NSLog(@"AVPlayerItemStatusFailed"); } } } @end
第二步,unity打包项目打包成xcode项目
Resources文件夹放入视频和图片资源:
文件名:logovideo.mp4和logovideo0.png,如需更改文件名或其他文件格式,需要在上面自定义View的代码中一起更改
Unity-iPhone 配置添加资源文件配置:
第三步,调用显示:
xcode项目中找到UnityAppController.m文件:
在开头引入AddViewSdk.h文件
找到startUnity方法,在指定位置调用**[[AddViewSdk GetInstance] showSplash]**,不可在之前调用
完成!!!!打包即可!!!
Cocos+u3d开发交流群:521643513
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。