超详细的iOS集成Unity项目

C#

浏览数:144

2019-8-30

AD:资源代下载服务

最近集成Unity项目时,看了好多这方面的文章,但是集成的过程中发现都不是很理想,总是出现这样那样的问题,自己摸索了好久,终于把流程理清了一遍。这里以一个简单的unity包为例,把这个流程走一遍,希望大家在集成unity时能够少踩坑。

一、项目准备

Unity项目
新建project

Unity目录结构

二、集成步骤

1、将以下选中几个文件/文件夹拖进新建的项目中


所需文件/文件夹

其中Data文件夹在拖入项目时,需要选择Creat folder references选项,其余的选择Creat groups选项。完成后效果如图所示:


完成效果

这个地方要注意的地方还有,看清自己copy文件放置的位置,即在哪个目录下,这点在后面的步骤中用到的时候会再说。

2、将Libraris文件夹下的libil2cpp文件夹引用删除,删除选项选择Remove References。

3、添加Unity项目所依赖动态库(每个人的项目所依赖的库可能是不同的,具体请参照Unity 导出的xCode 工程),并注意Optional 选项。


添加项目依赖库

4、Build Setting配置更改。此处重点需要注意以下几个位置,对照Unity项目一项一项进行设置。


Enable Bitcode
Other Linker Flags
Search Paths
Apple Clang – Custom Compiler Flags
关于C和C++的一些设置
pch文件的合并/将Class目录下的pch文件与自己项目中的pch文件合并,目录要写对

以上是对原有配置的更改,其中需要注意的地方有:
“Header Search Paths’ 添加Unity项目文件夹 Classes、Native、include 路径时一定要根据自己的项目路径来添加,否则会报找不到某些文件的错误。此处与前文提到的添加目录有关,需要特别注意。

5、添加User-Defined 配置


添加User-Defined配置位置
具体配置图例

具体添加配置如下:

GCC_THUMB_SUPPORT = NO
 GCC_USE_INDIRECT_FUNCTION_CALLS = NO
 UNITY_RUNTIME_VERSION = 2018.2.1f1  // 根据自己Unity 版本实际配置
 UNITY_SCRIPTING_BACKEND = il2cpp

6、添加Build Phases -> Run Script 配置,具体位置如下图:


Run Script 配置
Run Script 配置2

具体配置如下,但是必须得对应自己项目的目录结构,否则会报错。

"$PROJECT_DIR/MapFileParser.sh"
rm -rf "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data/Raw/QCAR"

7、main文件的合并
将Classes目录下的main.mm文件内容,替换到main.m文件中,并将main.mm文件引用删除,删除选项选择Remove References,main.m文件后缀改为.mm。

8、至此可能会出现的报错及相应的解决方式:
8.1 报错: No matching function for call to ‘objc_msgSend’
解决方式:Enable Strict Checking of objc_msgSend Calls ->NO
8.2 报错:Control may reach end of non-void function
解决方式:Mismatched Return Type -> NO
8.3 报错:Undefined symbols for architecture arm64:


诸如此类unity项目中方法未实现的原因造成的错误

解决方式:在任意一个.mm文件中添加如下代码,需要注意的是不要弄错其返回值。

extern "C"
{
 void UnitySendMsg(){
    }
}

9、入口修改

UnityAppController.h 

#import "AppDelegate.h"
inline UnityAppController*  GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    return delegate.unityController;
}

AppDelegate.h:

@class UnityAppController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIWindow *unityWindow;

@property (strong, nonatomic) UnityAppController *unityController;

//- (void)showUnityWindow;
- (void)hideUnityWindow;

/**  通用APP启动接口  */
-(void)commonInitLaunch;

/**  检查APP版本信息  */
-(void)checkAPPVerson;

+(AppDelegate *)sharedDelegate;

-(void)StartMyUnity3DGames; //开启unity

@end
AppDelegate.mm

#import <UIKit/UIKit.h>
#import "UnityAppController.h"

#import "AppDelegate.h"

-(UIWindow *)unityWindow{
    return  UnityGetMainWindow();
}

-(void)showUnityWindow{
    //进入unity界面
    //[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
    
    UnityPause(false);
    
    [self.unityWindow makeKeyAndVisible];
}

-(void)hideUnityWindow{
    
    //推出Unity界面
    //[(AppDelegate *)[UIApplication sharedApplication].delegate hideUnityWindow];
    
    UnityPause(true);
    
    [self.window makeKeyAndVisible];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.unityController = [[UnityAppController alloc] init];
    [_unityController application:application didFinishLaunchingWithOptions:launchOptions];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self commonInitLaunch]; // 原生项目启动方法
    
    return YES;
}

+(AppDelegate *)sharedDelegate{
    
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (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.
    
    [UIApplication sharedApplication].idleTimerDisabled = NO;
    
    [_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.
    
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    
    [_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.
    
    [[LoginManager instance] startALightWeightHttpServer];
    
    //[_unityController applicationDidBecomeActive:application];
    
}

-(void)StartMyUnity3DGames
{
    [_unityController applicationDidBecomeActive:[UIApplication sharedApplication]];
    
    [self showUnityWindow];
}


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

此时,已经将unity项目集成进自己新建的项目中了。如果还有什么报错,请自己检查下Build Setting中是否还有什么需要修改的地方。希望能对大家有所帮助。

作者:Adore_shine