当前位置:   article > 正文

Unity2018 iOS去除官方启动动画LOGO,播放自定义开场动画_unity2018去除启动logo插件

unity2018去除启动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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

创建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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

第二步,unity打包项目打包成xcode项目
Resources文件夹放入视频和图片资源:
文件名:logovideo.mp4和logovideo0.png,如需更改文件名或其他文件格式,需要在上面自定义View的代码中一起更改
在这里插入图片描述

Unity-iPhone 配置添加资源文件配置:
导入资源


第三步,调用显示:
xcode项目中找到UnityAppController.m文件:
在开头引入AddViewSdk.h文件
找到startUnity方法,在指定位置调用**[[AddViewSdk GetInstance] showSplash]**,不可在之前调用
在这里插入图片描述


完成!!!!打包即可!!!

Cocos+u3d开发交流群:521643513

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/91947
推荐阅读
相关标签
  

闽ICP备14008679号