IOS重写unity Splash,消除黑屏,播放开场动画视频

IOS重写unity Splash,消除黑屏,自定义开场动画视频,无需破解

此教程只使用于unity5.x系列,2018请看我新发的教程

因为自己使用的unity是免费版,无法去除splash,所以在IOS和Android添加了一个View遮挡unity的开场动画spalsh,这样既去除了黑屏,也可以播放视频等任意功能,在网上只找到Android版教程,所以自己模仿Android方法写了一个IOS的版本

Android教程:点击链接


IOS版教程:

首先将unity工程导出IOS项目,用Xcode打开

修改其中文件:

UnityAppController.h

新增方法

+(void)showMainView;

在这里插入图片描述


UnityAppController.mm

新增指针变量:

static UIWindow*  currentWindow  = nil;
static UIView*  currentRootView  = nil;
static UIViewController* currentRootController;

在这里插入图片描述

指针变量赋值:
在- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 方法里赋值

currentWindow = _window;
currentRootView = _rootView;
currentRootController = _rootController;

在这里插入图片描述

新增方法:

+(void)showMainView{
    
    
    [currentWindow addSubview: currentRootView];
    currentWindow.rootViewController = currentRootController;
    [currentWindow bringSubviewToFront: currentRootView];
    
    //指针释放,不知道对不对,平时很少写OC和C++
	currentRootView= nil;
	currentRootController = nil;
	currentRootView = nil;
	//
    HideStartAim();
}

在这里插入图片描述


SplashScreen.h

新增方法:

void HideStartAim();

在这里插入图片描述


SplashScreen.mm

增加变量:

static UIView* animView;

在这里插入图片描述

新增方法:

void HideStartAim()
{
    
    
    [animView removeFromSuperview];
    animView = nil;
}

在这里插入图片描述

修改create方法,延迟打开unity视窗,添加自定义View:

方法: - (void)create:(UIWindow)window*

- (void)create:(UIWindow*)window
{
    
    
    NSArray* supportedOrientation = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"UISupportedInterfaceOrientations"];
    bool isIphone = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
    bool isIpad = !isIphone;
    
    // splash will be shown way before unity is inited so we need to override autorotation handling with values read from info.plist
    _canRotateToPortrait            = [supportedOrientation containsObject: @"UIInterfaceOrientationPortrait"];
    _canRotateToPortraitUpsideDown  = [supportedOrientation containsObject: @"UIInterfaceOrientationPortraitUpsideDown"];
    _canRotateToLandscapeLeft       = [supportedOrientation containsObject: @"UIInterfaceOrientationLandscapeRight"];
    _canRotateToLandscapeRight      = [supportedOrientation containsObject: @"UIInterfaceOrientationLandscapeLeft"];
    
    CGSize size = [[UIScreen mainScreen] bounds].size;
    // iPads, iPhone 6+ and iPhone X have orientable splash screen. Also, looks like iOS 11 also has orientable
    // launch screen on all devices, but not updating this due to regression potential
    _isOrientable = isIpad || (size.height == 736 || size.width == 736) || (size.height == 812 || size.width == 812);
    
    // Launch screens are used only on iOS8+ iPhones
    const char* xib = UnityGetLaunchScreenXib();
#if !UNITY_TVOS
    _usesLaunchscreen = false;
    if (_ios80orNewer && xib != NULL)
    {
    
    
        const char* expectedName = isIphone ? "LaunchScreen-iPhone" : "LaunchScreen-iPad";
        if (std::strcmp(xib, expectedName) == 0)
            _usesLaunchscreen = true;
    }
#else
    _usesLaunchscreen = false;
#endif
    
    if (_usesLaunchscreen && !(_canRotateToPortrait || _canRotateToPortraitUpsideDown))
        _nonOrientableDefaultOrientation = landscapeLeft;
    else
        _nonOrientableDefaultOrientation = portrait;
    
    animView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, size.width, size.height)];
    animView.contentScaleFactor = [UIScreen mainScreen].scale;
    
    if (_isOrientable)
    {
    
    
        animView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        animView.autoresizesSubviews = YES;
    }
    else if (_canRotateToPortrait || _canRotateToPortraitUpsideDown)
    {
    
    
        _canRotateToLandscapeLeft = false;
        _canRotateToLandscapeRight = false;
    }
    // On non-orientable devices with launch screens, landscapeLeft is always used if both
    // landscapeRight and landscapeLeft are enabled
    if (!_isOrientable && _usesLaunchscreen && _canRotateToLandscapeRight)
    {
    
    
        if (_canRotateToLandscapeLeft)
            _canRotateToLandscapeRight = false;
        else
            _nonOrientableDefaultOrientation = landscapeRight;
    }
    
    self.view = animView;
    
#if !UNITY_TVOS
    self.wantsFullScreenLayout = TRUE;
#endif
    
    //读取本地视频路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"logovideo" ofType:@"mp4"];
    //为即将播放的视频内容进行建模
    AVPlayerItem *avplayerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];
    //创建监听(这是一种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.videoGravity = AVLayerVideoGravityResizeAspectFill;
    layer.frame = CGRectMake(0, 0, size.width, size.height);
    [animView.layer addSublayer:layer];
    
    [window addSubview:animView];
    //    [window addSubview: _splash];
    window.rootViewController = self;
    [window bringSubviewToFront:animView];
    [avplayer play];
}


UnityAppController+ViewHandling.mm

showGameUI注释三行

//    [_window addSubview: _rootView];
//    _window.rootViewController = _rootController;
//    [_window bringSubviewToFront: _rootView];

在这里插入图片描述


资源管理

Resources文件夹放入视频资源:
文件名:logovideo.mp4,如需更改文件名或其他视频格式,需要在上面自定义View的代码中一起更改
在这里图片描述
Unity-iPhone 配置添加视频文件:
在这里插入图片描述


unity端 调用这个方法,关闭开场动画页面,显示unity界面

 [UnityAppController showMainView];

unity调用IOS教程:点击链接

						*****************到此完成!************************

效果演示:

unity消除开始动画和黑边

猜你喜欢

转载自blog.csdn.net/weixin_40137140/article/details/103329442
今日推荐