I’m attempting to write my first unity script. This is the code for a file called TestPlugin.cs that is located in Assets/Plugins:
using UnityEngine;
using System.Runtime.InteropServices;
public class TestPlugin : MonoBehaviour
{
[DllImport ("__Internal")]
private static extern int getString ();
public static void Awake () {
print (getString ());
}
}
This is the code for two files that I import into the generated xCode project’s classes folder:
TestPlugin.h:
#import <Foundation/Foundation.h>
@interface TestPlugin : NSObject
-(int)getString;
@end
TestPlugin.m:
#import "TestPlugin.h"
@implementation TestPlugin
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (int)getString
{
return 7;
}
@end
Finally this is the javascript file that sits inside the Asset folder.
TestPluginTest.js:
function Update ()
{
TestPlugin.Awake ();
}
Also, please note that i’m not necessarily expecting this all to work, just to compile at this point (though extra pointers and tips are welcome)
The error I get in xCode when trying to build onto iPhone (actual device) is this:
Undefined symbols for architecture armv7: “_getString”, referenced
from:
RegisterMonoModules() in RegisterMonoModules.o ld: symbol(s) not found for architecture armv7 collect2: ld returned 1 exit status“_getString”, referenced from:
RegisterMonoModules() in RegisterMonoModules.o
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status
I’m stumped! Thanks in advance!
I think the problem lies in the Obj-C interface because the linker does not know how to handle the signature. When I connected a self written library I designed the interface to contain pure C code only:
interface.h
interface.c:
Maybe useful blog postings:
iPhone & Unity3D: Integrating 3rd Party Static Libraries in Unity3D Generated XCode Projects
Unity Native Plugins: OS X
Clever Martian’s Blog – An Experiment with iPhone Native UI and Unity 3 Pro