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

2019-10-15  本文已影响0人  猿王

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

注意:ios使用的Unity导出的工程,必须是在Mac系统下导出的Unity工程才可以,用windows系统导出的工程在ios中使用会出现问题。

一、准备工作

1.首先试运行Unity导出的工程是否可以运行(这里导出的工程是针对真机的,所以使用真机调试)

2.Unity导出工程时设置bundle id要与项目一致

3.准备一个Xcode创建的原生工程,这里使用的是一个Single View App。在ViewController 中创建一个按钮,以供点击进入Unity场景。

4.嵌入文件准备。在Unity导出的工程根目录下,将图中选中的文件copy进原生工程根目录下,

将Classes,Libraries,MapFileParser.sh拖入到项目(选中Copy items if needed, 选中Create groups)

将Data拖入到项目(选中Copy items if needed, 选中Create folder references)

image

5.删除Libraries->libil2cpp,这个文件的引用。

删除Classes->Native下“.h”文件的引用。

image

二、添加Framework引用库文件

1.添加Framework引用库文件,这里参考Unity导出工程,注意每个Framework对应的Status,这里有俩个库的Status是Optional。

image

关于libiconv.2.dylib的添加这里需要说一下,直接搜索,只有libiconv.2.tbd,所以选择“Add Other…”方式添加。

image

然后键盘快捷键CMD+Shift+G (go to folder) 输入 “/usr/lib”

image

继续搜索“libiconv”

image

#每个Unity导出的工程依赖的Framework不一样,这里依照需要嵌入的工程为准。

三、Build Settings

1.添加一个Run Script

#这里要注意路径,依照脚本文件所在路径为准。

image

2.Enable BitCode

因为这里Unity导出的工程不支持Bitcode,

image

3.添加Header Search Paths和Library Search Paths、Framework Search Paths

对应Unity导出工程添加

image

4.Other linker Flags 添加-weak_framework CoreMotion -weak-lSystem

特别注意:如iOS原生项目中配置了-all_load 应删除,否则会与Unity3D 项目中的依赖库冲突

image

5.Other C Flags、Other C++ Flags

在other C Flags 中添加 -DINIT_SCRIPTING_BACKEND=1 同是在 other C++ Flags中出现

根据Unity导出工程来修改

image

6.C Language Dialect 改为 C99

image

还有下图中四项,依照导出的工程修改

image

跟着下面的图片,对应Unity导出的工程做设置更改

image

7.添加User-Defined

点击“+”后,选择 “add User Defined Setting”

参考Unity 导出工程,这里我添加的有:

GCC_THUMB_SUPPORT NO

GCC_USE_INDIRECT_FUNCTION_CALLS NO

UNITY_RUNTIME_VERSION 5.4.0f3

UNITY_SCRIPTING_BACKEND il2cpp

image

注意:build Setting,修改主要是对照Unity导出的工程,先看Unity导出的工程的build Setting里那些项目是加粗的,这些就需要你注意对照原生工程修改了。

四、Pch文件合并与代码修改

合并Pch文件,将Classes->Prefix.pch内容拷贝入原生项目的Pch文件内,然后删除掉Classes->Prefix.pch。如果原生工程没有Pch文件,则可以直接使用这个Pch文件,记得去build Setting 里添加pch文件路径。

image image

添加 UnityAppController.h 的引用。

image

main文件修改,将Classes->main.mm文件内代码拷贝入原生工程目录下的main.m文件下,并且修改后缀为.mm,然后删除Classes->main.mm


//

//  main.m

//  lycUnitydemo

//

//  Created by pts on 2019/10/15.

//  Copyright © 2019 pt. All rights reserved.

//

#import 

#import "AppDelegate.h"

#include "RegisterMonoModules.h"

#include "RegisterFeatures.h"

#include<csignal>

// Hack to work around iOS SDK 4.3 linker problem

// we need at least one __TEXT, __const section entry in main application .o files

// to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation

staticconstintconstsection =0;

voidUnityInitTrampoline();

// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)

constchar* AppControllerClassName ="UnityAppController";

#if UNITY_USES_DYNAMIC_PLAYER_LIB

extern"C"voidSetAllUnityFunctionsForDynamicPlayerLib();

#endif

//const char* AppControllerClassName = "AppDelegate";

intmain(intargc,char* argv[])

{

#if UNITY_USES_DYNAMIC_PLAYER_LIB

    SetAllUnityFunctionsForDynamicPlayerLib();

#endif

    UnityInitStartupTime();

    @autoreleasepool

    {

        UnityInitTrampoline();

        UnityInitRuntime(argc, argv);

        RegisterMonoModules();

        NSLog(@"-> registered mono modules %p\n", &constsection);

        RegisterFeatures();

        // iOS terminates open sockets when an application enters background mode.

        // The next write to any of such socket causes SIGPIPE signal being raised,

        // even if the request has been done from scripting side. This disables the

        // signal and allows Mono to throw a proper C# exception.

        std::signal(SIGPIPE,SIG_IGN);

//        UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);

        UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }

    return 0;

}

#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

#include 

extern"C"intpthread_cond_init$UNIX2003(pthread_cond_t *cond,constpthread_condattr_t *attr)

{returnpthread_cond_init(cond, attr); }

extern"C"intpthread_cond_destroy$UNIX2003(pthread_cond_t *cond)

{returnpthread_cond_destroy(cond); }

extern"C"intpthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)

{returnpthread_cond_wait(cond, mutex); }

extern"C"intpthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,

                                               conststructtimespec *abstime)

{returnpthread_cond_timedwait(cond, mutex, abstime); }

#endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

//int main(int argc, char * argv[]) {

//    @autoreleasepool {

//        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

//    }

//}

原生 AppDelegate 中的修改

在pch文件中添加 #import "UnityAppController.h"。然后更改AppDelegate


#import 

@class UnityAppController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic , strong)UIWindow *unityWindow;

@property (nonatomic , strong)UnityAppController *unityVC;

- (void)showUnityWindow;

- (void)hideUnityWindow;

- (void)shouldAttachRenderDelegate;

@end

在AppDelegate.mm(因为有混编,本来变一个main.mm文件就可以了。这里也变过来双保险)


#import "AppDelegate.h"

#import "UnityAppController.h"

//extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType);

//extern "C" void VuforiaRenderEvent(int marker);

@interface AppDelegate ()

{

    UIButton*_backBtn;

}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    // Override point for customization after application launch.

    //unity

    self.unityVC = [[UnityAppController alloc] init];

    [self.unityVC application:application didFinishLaunchingWithOptions:launchOptions];

    [self.window makeKeyAndVisible];

    return YES;

}

- (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.

    [self.unityVC 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.

    [self.unityVC 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.

    [self.unityVC 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.

    [self.unityVC applicationDidBecomeActive:application];

}

- (void)applicationWillTerminate:(UIApplication*)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    [self.unityVC applicationWillTerminate:application];

}

#pragma mark -

#pragma mark ---------------unity开启与隐藏

- (UIWindow*)unityWindow

{

    if (!_unityWindow) {

        _unityWindow = UnityGetMainWindow();

    }

    return _unityWindow;

}

- (void)showUnityWindow

{

    [self.unityWindow makeKeyAndVisible];

    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [_backBtn setImage:[UIImage imageNamed:@"返回icon"] forState:UIControlStateNormal];

    _backBtn.backgroundColor =[UIColor redColor];

    [self.unityWindow addSubview:_backBtn];

    [_backBtn addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];

    _backBtn.frame=CGRectMake(15,30,50,50);

//    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make)

//    {

//        make.top.equalTo(_unityWindow).offset(30);

//        make.left.equalTo(_unityWindow).offset(15);

//        make.width.mas_equalTo(50);

//        make.height.mas_equalTo(50);

//    }];

}

- (void)hideUnityWindow

{

    [self.window makeKeyAndVisible];

}

- (void)shouldAttachRenderDelegate

{

//    UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);

    //如果两个都报错,那就都删掉,这个方法就做空实现。上面那两个extern "C"都可以删掉了

    UnityRegisterRenderingPlugin(NULL, NULL);

}

@end

修改UnityAppController.h 中的

先引入#import "AppDelegate.h"


inlineUnityAppController* GetAppController()

{

//    return _UnityAppController;

    AppDelegate *dele = (AppDelegate *)[UIApplication sharedApplication].delegate;

    return (UnityAppController *)dele.unityVC;

}

修改UnityAppController.mm 中的

- (void)shouldAttachRenderDelegate  {
    AppDelegate *deleg = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [deleg shouldAttachRenderDelegate];
}

错误调试:

如果有:

Undefined symbols for architecture arm64:

"**********", referenced from:DynamicLibEngineAPI.o

则去 Build Phases 搜索 DynamicLibEngineAPI 然后删除引用

Undefined symbols for architecture armv7

遇到这个错的同学请添加AssetsLibrary.framework和Accelerate.framework

image

编译时遇到Permission denied错误的是因为当前开发账号对项目目录没有权限执行MapFileParser.sh

解决办法: 在终端执行命令:(chmod +x 你的MapFileParser.sh的路径)

如:chmod +x /Users/rge/Downloads/IOSProject/MapFileParser.sh

Control reaches end of non-void function

解决办法: 把Mismatched Return Type 改为NO

image image

C++混编需要设置的地方

VuforiaRenderEvent和VuforiaSetGraphicsDevice报错

把AppDelegate.mm中的屏蔽掉并修改shouldAttachRenderDelegate


//extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType);

//extern "C" void VuforiaRenderEvent(int marker);


- (void)shouldAttachRenderDelegate

{

//    UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);

    //如果两个都报错,那就都删掉,这个方法就做空实现。上面那两个extern "C"都可以删掉了

    UnityRegisterRenderingPlugin(NULL, NULL);

}

MapFileParser.sh: No such file or directory

是因为Build Phases里的Run Script 设置的路径不对,在项目左侧文件Show in Finder,查看到正确位置,并设置就可以了。

image

参考内容:

unity导出工程导入到iOS原生工程中详细步骤 - 懒懒初阳 - 博客园

[转]Unity与iOS交互 - 简书

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

iOS - 将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法 - baidu_25743639的专栏 - CSDN博客

上一篇下一篇

猜你喜欢

热点阅读