工具:
Unity版本:Unity 5.4.3f1
Xcode版本:Xcode 9.3.1
真机
一、导出iOS平台的Unity项目
-
编译设置:File -> Build Settings
-
设置公司名称&产品名称
-
iOS项目的配置文件
-
开始编译
-
文件目录
二、Xcode集成Unity项目(两种方式)
- 将原有iOS项目集成至unity项目中
- 将原工程直接拖拽到Unity生成的工程里,在Unity的工程上进行开发。
- 将原工程的main.m文件、AppDelegate.h和AppDelegate.m文件删除,若有pch文件,也可与Unity工程中的Pch文件合并后删除(删除相关文件前请先备份,合并后请检查pch路径是否正确)。
- 此时程序的主函数入口为Classes/main.mm,AppDelegate为Classes/UnityAppController.mm,程序的pch文件为Classes/Prefix.pch。将原工程的相应代码拷贝到相对应的文件中即可。
- 因为现在程序的入口在Unity工程端,所以原工程的主界面需在Unity工程运行后,再添加到keyWindow上即可。
- 将unity项目集成至原有iOS项目中
- Unity导出的工程中的Bundle Identifier要和你本地iOS项目保持一致
- TARGETS -> Build Settings -> Build Options -> Enable Bitcode设置为NO
(Unity自带的静态库:libmml.a等不支持bitcode) - 删除Main.storyboard,代码设置控制器(方便切换window)
- 将Classes,Libraries,MapFileParser.sh拖入到项目(选中Copy items if needed,选中Create groups);
删除Classes/Native文件夹下的所有.h文件的引用;
删除Libraries文件夹下的libil2cpp和bdwgc文件夹的引用;
注意一定要选择Remove Reference,千万不要选择Move to Trash!(下同) - 将Data拖入到项目(选中Copy items if needed,选中Create folder references)
- 将Classes中的
iPhone_target_Prefix.pch
合并到项目原有的pch文件中后删除该文件引用;
若原项目中没有,准确设置pch文件路径即可(TARGETS -> Build Settings -> Apple LLVM 9.0 - Language -> Prefix Header :.pch file path
)
// iPhone_target_Prefix.pch
//
// Prefix header
//
#ifdef __OBJC__
#import
#import
#endif
#include "Preprocessor.h"
#include "UnityTrampolineConfigure.h"
#include "iPhone_Common.h"
#include "UnityInterface.h"
#ifdef __cplusplus
extern "C" {
#endif
void UnitySendMessage(const char* obj, const char* method, const char* msg);
#ifdef __cplusplus
}
#endif
#define printf_console printf
- 删除Classes中的main.mm文件,修改Supporting Files中的main.m为main.mm(可以将main.m中的代码替换为main.mm的代码,具体视情况而定,main函数之后调用UnityAppController.mm,也就是以前项目的AppDelegate.m文件不调用,可以删除)
// main.mm
#import
#include "RegisterClasses.h"
#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
#include
// 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
static const int constsection = 0;
void UnityInitTrampoline();
#if INIT_SCRIPTING_BACKEND
extern "C" void InitializeScriptingBackend();
#endif
// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)
const char* AppControllerClassName = "UnityAppController";
int main(int argc, char* argv[])
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
UnityInitTrampoline();
if(!UnityParseCommandLine(argc, argv))
return -1;
#if INIT_SCRIPTING_BACKEND
InitializeScriptingBackend();
#endif
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]);
[pool release];
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#if TARGET_IPHONE_SIMULATOR
extern "C" clock_t clock$UNIX2003() { return clock(); }
extern "C" void closedir$UNIX2003(DIR *dirp) { closedir(dirp); }
extern "C" FILE* fdopen$UNIX2003(int fildes, const char* mode) { return fdopen(fildes, mode); }
extern "C" FILE* fopen$UNIX2003(const char* path, const char* mode) { return fopen(path, mode); }
extern "C" int fputs$UNIX2003(const char* s, FILE* stream) { return fputs(s, stream); }
extern "C" size_t fwrite$UNIX2003(const void* ptr, size_t size, size_t nitems, FILE* stream) { return fwrite(ptr, size, nitems, stream); }
extern "C" time_t mktime$UNIX2003(tm* timeptr) { return mktime(timeptr); }
extern "C" int nanosleep$UNIX2003(const timespec* rqtp, timespec* rmtp) { return nanosleep(rqtp, rmtp); }
extern "C" DIR* opendir$INODE64$UNIX2003(const char* dirname) { return opendir(dirname); }
extern "C" dirent* readdir$INODE64(DIR* dirp) { return readdir(dirp); }
extern "C" ssize_t recv$UNIX2003(int socket, void* buffer, size_t length, int flags) { return recv(socket, buffer, length, flags); }
extern "C" ssize_t send$UNIX2003(int socket, const void *buffer, size_t length, int flags) { return send(socket, buffer, length, flags); }
extern "C" int setenv$UNIX2003(const char* envname, const char* envval, int overwrite) { return setenv(envname, envval, overwrite); }
extern "C" int sigaltstack$UNIX2003(const stack_t* ss, stack_t* oss) { return sigaltstack(ss, oss); }
extern "C" unsigned sleep$UNIX2003(unsigned seconds) { return sleep(seconds); }
extern "C" char *strerror$UNIX2003(int errnum) { return strerror(errnum); }
extern "C" size_t strftime$UNIX2003(char* s, size_t maxsize, const char* format, const tm* timeptr) { return strftime(s, maxsize, format, timeptr); }
extern "C" int system$UNIX2003(const char* command) { return system(command); }
extern "C" int unsetenv$UNIX2003(const char* name) { return unsetenv(name); }
extern "C" pid_t waitpid$UNIX2003(pid_t pid, int *stat_loc, int options) { return waitpid(pid, stat_loc, options); }
#endif // TARGET_IPHONE_SIMULATOR
- 在Build Phases新建运行脚本语句Run Script
rm -rf "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"
cp -Rf "$PROJECT_DIR/Data" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"
- 在Build settings中设置Header Search Paths & Library Search Paths (具体路径视Unity项目路径有所不同)
// Header Search Paths
// non-recursive
$(SRCROOT)
$(SRCROOT)/Classes
$(SRCROOT)/Classes/Native
$(SRCROOT)/Libraries/bdwgc/include
$(SRCROOT)/Libraries/libil2cpp/include
// Library Search Paths
// non-recursive
$(inherited)
$(SRCROOT)
$(SRCROOT)/Libraries
- 在Build Settings中设置Other C Flags
-mno-thumb
-DINIT_SCRIPTING_BACKEND=1
- 在Build Settings中设置C Language Dialect
C99[-std=c99]
-
在Build Phases中导入以下库(这里具体请查看导出的ios工程里面的库,千万不要重复导入;注意有的库的Status需要设置为Optional,避免在低版本中奔溃!)
- 在Build Phases中将Unity项目中所有.mm文件(除了CameraCapture.mm,MetalHelper.mm,DisplayManager.mm文件以外)打上MRC标签
// 设置Compiler Flags如下:
-fno-objc-arc
参考:
iOS原生工程接入Unity导出的Xcode工程
unity导出工程导入到iOS原生工程中详细步骤
史上最全unity与xcode集成教程
unity3D与iOS原生工程项目合并以及合并过程中的问题