简体   繁体   English

如何在Unity中使用iOS框架?

[英]How to use iOS framework with Unity?

I'm trying to use an iOS native framework inside Unity, but I don't know how. 我正在尝试在Unity中使用iOS本机框架,但我不知道如何。

Inside my C# script, I call a native function like this : 在我的C#脚本中,我调用这样的本机函数:

[DllImport ("__Internal")]
    private static extern void writeHello();

    void Start () {
        writeHello();
    }

I have drag the iOS framework inside my project like this : 我已经在我的项目中拖动iOS框架,如下所示:

Link to my image 链接到我的图像

But when I deploy my app on iPad, xCode showing this error : 但是当我在iPad上部署我的应用程序时,xCode显示此错误:

Undefined symbols for architecture armv7: "_writeHello", referenced from: RegisterMonoModules() in RegisterMonoModules.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation) 架构armv7的未定义符号:“_ writeHello”,引用自:RegisterMonoModules中的RegisterMonoModules()ld:未找到架构armv7 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

I don't understand why, and I don't know if what I'm trying to do is possible. 我不明白为什么,我不知道我想做什么是可能的。 Help! 救命! :) :)

Thanks, Sennin 谢谢,Sennin

The iOS framework must be put in the xCode generated project, not in unity. iOS框架必须放在xCode生成的项目中,而不是统一。

And the framework's functions must be wrapped in an extern "C" block (as shown here ). 和框架的功能必须被包裹在一个extern "C"块(如图这里 )。

Then you will be able to use it in C# with dllimport . 然后你就可以在C#中使用dllimport

The doc says that all files with extensions .a,.m,.mm,.c,.cpp located in the Assets/Plugins/iOS folder will be merged into the generated Xcode project automatically. 该文档说,位于Assets / Plugins / iOS文件夹中的扩展名为.a,.m,。mm,.c,.cpp的所有文件将自动合并到生成的Xcode项目中。 The docs also says that subfolders are currently not supported, so, in your example, the "framework-helloUnity.framework" folder will be ignored. 文档还说目前不支持子文件夹,因此,在您的示例中,将忽略“framework-helloUnity.framework”文件夹。

It is important to note that .m files default to C-style bindings, while .mm files default to C++-style bindings. 重要的是要注意.m文件默认为C风格的绑定,而.mm文件默认为C ++风格的绑定。 In other word, unless you use the extern "C" keyword, as Heilo suggested, your functions won't be found if you put them in .mm or .cpp files. 换句话说,除非你使用extern "C"关键字,正如Heilo建议的那样,如果你将它们放在.mm或.cpp文件中,你的函数将无法找到。

In other words, if you put the following: 换句话说,如果你把以下内容:

void writeHello() {}

in a file named Assets/Plugins/iOS/myTestFile.m , your code should compile. 在名为Assets/Plugins/iOS/myTestFile.m ,您的代码应该编译。

Found the solution thanks to this post : Calling Objective-C method from C++ method? 通过这篇文章找到了解决方案: 从C ++方法调用Objective-C方法?

By doing like that : 通过这样做:

void myWriteHello( void *test) {
    [(id)test writeHello];
}

- (void) writeHello
{
    NSLog(@"Hello World!");
}

and calling my C function from Unity 并从Unity调用我的C函数

You have write the following function in your Xcode project any class extern "C" { -(void) writeHello { 您已在Xcode项目中编写以下函数,任何类extern“C”{ - (void)writeHello {

} } }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM