c# – Unity的Objective C iOS插件
我在使用我制作的Objective C插件在
Xcode中构建Unity3d项目(在设备上测试)时遇到了问题.
这是文件: TestPlugin.h文件: #import <Foundation/Foundation.h> @interface TestPlugin : NSObject +(int)getGoodNumber; @end TestPlugin.m文件: #import "TestPlugin.h" @implementation TestPlugin +(int)getGoodNumber { return 2222211; } @end 最后是团结的C#脚本,它应该打印出getGoodNumber()返回的值: using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class PluginTest : MonoBehaviour { [DllImport ("__Internal")] public static extern int getGoodNumber(); void OnGUI() { string goodNum = getGoodNumber().ToString(); GUILayout.Label (goodNum); } } 我可以告诉我,代码不应该有任何问题.但即使我遵循了许多不同的教程,当我尝试编译时,我在Xcode中得到一个错误: Undefined symbols for architecture armv7: "_getGoodNumber",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) 我尝试了一百万种不同的东西,似乎没有任何帮助.尽管我可以从其他教程中读到,但我不需要对Xcode进行任何特殊设置,我可以将它们保留为没有插件的Unity项目. 我还想澄清一些事情: >插件文件位于Unity3d的/ Plugins / iOS /文件夹中 如果有人遇到了解决它的问题,我很乐意听到解决方案. 解决方法
你已经编写了一个“objective-c”类和方法,但是不能向Unity公开.您需要创建一个“c”方法(如果需要,可以调用objective-c方法).
例如: plugin.m: long getGoodNumber() { return 111; } 这是一个更全面的例子,演示了获得陀螺仪的参数. 让我们让运动经理得到陀螺仪(现在假装).这将是标准目标-c: MyMotionManager.h @interface MyMotionManager : NSObject { } +(MyMotionManager*) sharedManager; -(void) getGyroXYZ:(float[])xyz; @end MyMotionManager.m: @implementation MyMotionManager + (MyMotionManager*)sharedManager { static MyMotionManager *sharedManager = nil; if( !sharedManager ) sharedManager = [[MyMotionManager alloc] init]; return sharedManager; } - (void) getGyroXYZ:(float[])xyz { // fake xyz[0] = 0.5f; xyz[1] = 0.5f; xyz[2] = 0.5f; } @end 现在让我们通过C外部引用公开它(不需要extern,因为它在.m(不是.mm)中: MyMotionManagerExterns.m: #import "MyMotionManager.h" void GetGyroXYZ(float xyz[]) { [[MyMotionManager sharedManager] getGyroXYZ:xyz]; } 最后,在Unity C#中调用它: MotionPlugin.cs: using UnityEngine; using System; using System.Collections; using System.Runtime.InteropServices; public class MotionPlugin { [DllImport("__Internal")] private static extern void GetGyroXYZ(float[] xyz); public static Vector3 GetGyro() { float [] xyz = {0,0}; GetGyroXYZ(xyz); return new Vector3(xyz[0],xyz[1],xyz[2]); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |