Unity2018iOS去除官⽅启动动画LOGO,播放⾃定义开场动画
因为项⽬从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;
}unity 教程
-(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];
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*)conte xt{
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论