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

时间:2024-04-05 08:54:54

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

demo下载地址 http://pan.baidu.com/s/1pLcpKpl


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


2.修改bit codeNO


3.删除Main.storyboard,代码设置控制器(方便切换window)


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

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


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


5.添加framework(最下边三个是HiAR UnitySDK里边的)


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



6.添加 Header Search Paths  Library Search Paths


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



7.other C Flags -> -DINIT_SCRIPTING_BACKEND=1


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




8.添加User-Defined (UNITY_RUNTIME_VERSION版本号与导出工程的Unity版本号一致)


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

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



9.Unity/Classes/Prefix.pch 的代码复制到新建的pch文件, 设置pch文件路径


10.将Classes/main.mm全部内容复制到main.m 并把扩展名改为.mm, 修改如下代码


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


11.添加Run Script


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



12. 修改UnityAppController.h如下代码

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


13.在AppDelegate.h .m中添加如下代码


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






在集成过程中可能遇到的问题


1.Unknown type name 'UnityViewControllerBase'

UnityViewControllerBase报错, 把它改成UIViewController


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


2. Unknown type name 'AppDelegate'

在新建的项目里边不会报错, 但是我集成到公司项目的时候报这个错, 于是改成如下代码, 目前没有出现问题, 如果有人知道原因, 请在评论区告诉我


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


3. Undefined symbols for architecture armv7

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


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


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

解决办法: 在终端执行命令 sudo chmod -R 777 所在的目录


5. Functions that differ only in their return type cannot be overloaded

解决办法: 把Enable Strict Checking of objc_msgSend Calls 改为NOiOS - 将Unity导出的Xcode工程导入到另一个Xcode项目, 及常见报错的解决方法

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


6. Control reaches end of non-void function

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

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

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

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


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

 7.删除unity自带的.pch和main.mm文件



8.linker command failed with exit code 1

duplicate symbol __Z32RegisterAllStrippedInternalCallsv in:

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/UnityICallRegistration.o

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/RegisterMonoModules.o

duplicate symbol __Z18RegisterAllClassesv in:

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/UnityClassRegistration.o

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/RegisterMonoModules.o

duplicate symbol __Z43InvokeRegisterStaticallyLinkedModuleClassesv in:

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/UnityClassRegistration.o

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/RegisterMonoModules.o

duplicate symbol __Z39RegisterStaticallyLinkedModulesGranularv in:

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/UnityClassRegistration.o

    /Users/xinshang/Library/Developer/Xcode/DerivedData/App3.0-hidvomkzbvfqvibtxzyzngoflknc/Build/Intermediates/App3.0.build/Debug-iphoneos/App3.0.build/Objects-normal/arm64/RegisterMonoModules.o

ld: 4 duplicate symbols for architecture arm64

clang: error: linker command failed with exit code 1(use -v to see invocation)


解决办法:

找到对应的RegisterMonoModules.cpp文件,注释掉重复的方法:

#if !INIT_SCRIPTING_BACKEND


extern void RegisterAllClassesGranular();

//void RegisterAllClasses()

//{

//    // Register classes for unit tests

//    RegisterAllClassesGranular();

//}

//

//void RegisterAllStrippedInternalCalls() {}

//

//void InvokeRegisterStaticallyLinkedModuleClasses() {}

//void RegisterStaticallyLinkedModulesGranular() {}


#endif // INIT_SCRIPTING_BACKEND


void RegisterMonoModules() {}



9.[DYMTLInitPlatform] platform initialization successful启动报错,

解决:修改main.m文件


//  main.m

//  App3.0

//

//  Created by mac on 17/2/16.

//  Copyright © 2017 mac. All rights reserved.

//



#include "RegisterMonoModules.h"

#include "RegisterFeatures.h"

#include <mach/mach_time.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

static const int constsection = 0;


void UnityInitTrampoline();


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

//const char* AppControllerClassName = "UnityAppController";

const char* AppControllerClassName = "AppDelegate";



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

{

    signed long long startTime = mach_absolute_time();

    @autoreleasepool

    {

        UnitySetStartupTime(startTime);

        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]);

    }

    

    return 0;

}


#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR


#include <pthread.h>


extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)

{ return pthread_cond_init(cond, attr); }

extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)

{ return pthread_cond_destroy(cond); }

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

{ return pthread_cond_wait(cond, mutex); }

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

                                               const struct timespec *abstime)

{ return pthread_cond_timedwait(cond, mutex, abstime); }


#endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

// 可能有的步骤忘记了, 有疑问的可以在评论区留言