原生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
相關文章
相關標籤/搜索