在原生iOS集成Unity3D应用

在原生iOS集成Unity3D应用

在原生iOS集成Unity3D,可参考:

视频教程可参考:Integrating Unity3D with native iOS application,貌似比较老了,不过还是很有参考价值

记录下集成过程中遇到的问题:

1.对照Unity导出的iOS项目工程,很多设置可以直接拷贝过来,不用一个一个输入

2.首先要明白$(SRCROOT)的含义,可参考iOS开发之Xcode 相对路径与绝对路径

$(SRCROOT)宏和$(PROJECT_DIR)宏都指xxx.xcodeproj所在的父目录

在设置Header Search PathsLibrary Search Paths时,可能需要这个宏

3.Other C FlagsOther C++ Flags都要设置

-DINIT_SCRIPTING_BACKEND=1
-fno-strict-overflow
-DRUNTIME_IL2CPP=1

我当时是只设置了Other C Flags,未设置Other C++ Flags,导致提示cannot use 'try' with exceptions disabled

4.可能会提示__weak typeof(self) wSelf这种类型的错误,参考typeof 编译错误

__weak typeof(self) wSelf = self报错:- A parameter list without types is only allowed in a function definition. A corresponding warning tells me that __weak only applies to Objective-C object or block pointer types; type here is ‘int’

在教程中我们是将C Language Dialect设置为C99 [-std=c99],将其改成GNU99即可

5.提示

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x146746d0>) doesn't contain a view controller with identifier 'unitySplashStoryboard''

按教程中的说明修改:

解决办法如下:在SplashScreen.mm文件修改下面的方法
void ShowSplashScreen(UIWindow* window)
{
//    bool hasStoryboard = [[NSBundle mainBundle] pathForResource: @"LaunchScreen" ofType: @"storyboardc"] != nullptr;
//
//    if (hasStoryboard)
//    {
//        UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"LaunchScreen" bundle: [NSBundle mainBundle]];
//        _controller = [storyboard instantiateViewControllerWithIdentifier: @"unitySplashStoryboard"];
//    }
//    else
        _controller = [[SplashScreenController alloc] init];

    [_controller create: window];
    [window makeKeyAndVisible];
}

6.如下的几个方法一定要实现,否则unity会不显示

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    [_unityController applicationWillResignActive:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [_unityController applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [_unityController applicationWillEnterForeground:application];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [_unityController applicationDidBecomeActive:application];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [_unityController applicationWillTerminate:application];
}

7.导入Unity后,原来的动画被disable掉了 or 加载的HUD消失了,可参考解决整合原生xcode工程和unity3D发布的iOS工程导航栏推出视图控制器失去动画效果的坑


在集成Unity应用后,遇到了一些问题,记录如下:

1.无网络(关闭wifi和移动网络)时crash

按上面提到的教程集成Unity,在app启动时,初始化unity,暂时先隐藏unity,在需要的时候,再显示unity。但是如果,启动的时候,关闭掉wifi和移动网络,app一启动有很大的概率crash

使用umeng捕获的异常如下:

Application received signal SIGBUS
(null)
((
	0   CoreFoundation                      0x000000019081d1d0 <redacted> + 148
	1   libobjc.A.dylib                     0x000000018f25455c objc_exception_throw + 56
	2   CoreFoundation                      0x000000019081d100 <redacted> + 0
	3   CureFunNew!UmengSignalHandler
	4   libsystem_platform.dylib            0x000000018f8ad338 _sigtramp + 36
	5   CureFunNew!Job_ExecuteUnityWebRequest [dynamic_array.h : 473]
	6   CureFunNew!Exec [JobQueue.cpp : 386]
	7   CureFunNew!Steal [JobQueue.cpp : 613]
	8   CureFunNew!ExecuteJobFromQueue [JobQueue.cpp : 770]
	9   CureFunNew!ProcessJobs [JobQueue.cpp : 823]
	10  CureFunNew!WorkLoop [JobQueue.cpp : 909]
	11  CureFunNew!RunThreadWrapper [thread.cpp : 36]
	12  libsystem_pthread.dylib             0x000000018f8b1850 <redacted> + 240
	13  libsystem_pthread.dylib             0x000000018f8b1760 <redacted> + 0
	14  libsystem_pthread.dylib             0x000000018f8aed94 thread_start + 4
)

使用真机来调试,也捕获到如上的异常:

unity异常

集成非模拟器的unity包后,貌似不能在模拟器上运行调试了,而且使用真机调试unity的时候,也会直接卡住,如何解决呢?
如下,Metal API Validation 设置为 disabled

edit schema

可参考:

至于如何解决启动时的崩溃,可以尝试,不要在app启动的时候,加载unity

2.如何展示unity的问题

在展示untiy的时候,通常会要处理旋转问题,一般的app只支持portrait,而unity通常则需要横屏

如果unity中不需要弹出键盘,即不需要处理键盘的方向的话,可直接把view旋转

如果需要处理键盘的方向,可重写AppDelegateapplication:supportedInterfaceOrientationsForWindow:的方法

可参考:

另外在这篇文章:将Unity3D 添加到IOS具体页面 以及横屏显示中,使用的是模态弹出unity,这样做的优点是,可以直接控制旋转,非常有参考的价值

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:白松林 返回首页