Unity2018 iOS去除官方啓動動畫LOGO,播放自定義開場動畫

由於項目從5.x升級到2018版,發現之前發的教程無效了,只適用5.x,因此從新寫了個方法。xcode

第一步找到unity 項目\Assets\Plugins\iOS文件夾,沒有文件夾則建立一個。
在文件夾內建立oc類:
ide

建立頭文件AddViewSdk.hatom

#import <Foundation/Foundation.h>


@interface AddViewSdk:NSObject
+(AddViewSdk *) GetInstance;
-(void) showSplash;
-(void) hiSplash;
@end

建立AddViewSdk.m文件:spa

// 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的代碼中一塊兒更改
在這裏插入圖片描述


code

Unity-iPhone 配置添加資源文件配置:
導入資源
視頻


第三步,調用顯示:
xcode項目中找到UnityAppController.m文件:
在開頭引入AddViewSdk.h文件
找到startUnity方法,在指定位置調用**[[AddViewSdk GetInstance] showSplash]**,不可在以前調用
在這裏插入圖片描述



server


完成!!!!打包便可!!!對象

相關文章
相關標籤/搜索