原生iOS嵌入Unity导出的Xcode工程

因为最近上有相似需求 因此这两天研究了下。xcode

1、准备工做

unity导出的xcode项目

2、开始倒腾

一、将Unity3D中的一下文件导入到工程目录下

  • Data
  • Classes
  • MapFileParser.sh
  • Libraries
  • MapFileParser执行文件

注意:Data文件导入类型为Creat folde references Classes MapFileParser.sh Libraries MapFileParser执行文件 经过Creat groups导入app

导入后以下图所示ide

二、删除引用

  • 删除Libraries->libil2cpp的引用 选项为Remove Refernces
  • target -> Build Phases -> DynamicLibEngineAPI 移除。若是不移除 会报错Undefined symbols for architecture arm64: ...in ... from:DynamicLibEngineAPI.o

三、设置classes->prefix.pch添加pch路径

四、main.m操做

将classes中main.mm 中的代码复制到项目的main.m中 并把后缀也改成mm 并将ui

UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
复制代码

修改成atom

UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: @"AppDelegate"]);
复制代码

5 修改AppDelegate

AppDelegate.hspa

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
@property (strong, nonatomic) UnityAppController *unityController;

-(void)StartUnity; //开启unity
- (void)hideUnityWindow; //暂停

复制代码

AppDelegate.mcode

#import "AppDelegate.h"
#import "SRViewController.h"
#import "UnityAppController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

-(void)showUnityWindow{
    UnityPause(false);
}

-(void)hideUnityWindow{
    UnityPause(true);
}

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


- (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 redColor];
    SRViewController *vc = [[SRViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    [_unityController applicationWillResignActive:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_unityController applicationDidEnterBackground:application];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [_unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_unityController applicationDidBecomeActive:application];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    [_unityController applicationWillTerminate:application];
}
复制代码

6 UnityAppController.h

orm

inline UnityAppController* GetAppController()
{
    return _UnityAppController;
}
复制代码

修改成cdn

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

7 调用

@implementation SRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"123";
    self.view.backgroundColor = [UIColor greenColor];
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 400, 100, 50)];
    [btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"开始" forState:UIControlStateNormal];
    btn.tag = 1;
    [self.view addSubview:btn];
    
    
    UIButton * btn2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 450, 100, 50)];
    [btn2 addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 2;
    [btn2 setTitle:@"结束" forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    
}

- (void)test:(UIButton *) btn{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    delegate.unityWindow.frame = CGRectMake(0, 0, 100, 200);
    
    if (btn.tag == 1) {
        [self.view addSubview:delegate.unityWindow];
        delegate.unityWindow.hidden = NO;
        [delegate StartMyUnity];
    }else {
        [delegate hideUnityWindow];
        delegate.unityWindow.hidden = YES;
    }
    
}

复制代码

3、添加Framework以及Run Script

添加这两项的时候注意和unity的工程中保持一致blog

  • Frameworkork 注意事项: 注意Status是Required仍是Optional
  • libiconv.2.dylib 经过 Add Other -> command+shift+G 搜索/usr/lib 找到libiconv.2.dylib添加

4、其余重要设置

  • Enable BitCode = NO;
  • Header/Library Search Paths 和unity保持一致
  • Framework Search Paths 和unity保持一致
  • Other linker Flags 和unity保持一致
    • all_load 若是项目中有这个 记得删除 和unity不兼容
  • Supported Platforms 根据unity导出项目保持一致 最好真机
  • Other C Flags、Other C++ Flags 和untiy保持一致
  • C Language Dialect 保持一致
  • 添加User-Defined (target->Build Settings -> Leveles右侧的加号)
    • GCC_THUMB_SUPPORT = NO
    • PROVISIONING_PROFILE
    • UNITY_RUNTIME_VERSION = unity版本号
    • UNITY_SCRIPTING_BACKEND = il2cpp
相关文章
相关标签/搜索