因為專案從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]**,不可在之前呼叫
完成!!!!打包即可!!!