0
\$\begingroup\$

I want to execute native iOS code in unity. For this I have added two files in /plugins/ios.

  1. ViewController.h
  2. ViewController.m

Code for each file represented as under.

ViewController.h

@interface ViewController

-(void)sampleMethod;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

-(void)sampleMethod
{
     NSLog(@"Sample Method Execute");
}
@end

For C# file I have added following code

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

and call to above method

sampleMethod();

When I am export project for iOS, xCode give me following error that I can't able to understand.

enter image description here

I can't able to understand how to solve this problem? Please give some suggestion here.

EDIT :

I have added extern keyword in my .m file after reading some suggestion.

#import "ViewController.h"

@implementation ViewController

extern "C"
{
   -(void)sampleMethod
   {
        NSLog(@"Sample Method Execute");
   }
}
@end

But in xCode now I am getting Expected Identifier or '(' at the line of extern keyword line. So what to do next?

\$\endgroup\$

closed as off-topic by MichaelHouse Jun 5 '15 at 17:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

Right now I'm not in a place to go over your code thoroughly, but at a glance I'm wondering if you need to declare your code with C linkage as explained here:

extern "C" {
  float FooPluginFunction();
}
\$\endgroup\$
  • \$\begingroup\$ I have edited my question with some more detail after reading your suggestion. \$\endgroup\$ – Siddharth Nov 3 '14 at 12:59
  • \$\begingroup\$ I think you may want to remove the function from both your interface and the implementation. I'm not certain (just comparing against my own working plugin, haven't done this in a while) but I think you need to have the extern outside of the class implementation (ie. below @end) \$\endgroup\$ – jhocking Nov 3 '14 at 14:36
  • \$\begingroup\$ Okay I will test this and reply you shortly. \$\endgroup\$ – Siddharth Nov 4 '14 at 9:46
0
\$\begingroup\$

I'm not sure if this answers your question but:

iOS of course doesn't run Java since iOS requires all code to be compiled AOT to xcode / objective c. When you build an iOS project, Unity just creates an xcode project which then can be built using xcode. You are free to change the code that Unity has cross compiled, however you have to do this every time when you rebuild the xcode project from Unity.

It's probably possible to compile a native library for iOS that you can add to the Unity project itself as "plugin". However i'm not familiar with iOS development.

http://answers.unity3d.com/questions/816358/executing-native-ios-code-in-unity.html

\$\endgroup\$
  • \$\begingroup\$ Link only answers make poor answers and is against the goal of the site. Please expand your answer to include enough detail to answer this question without requiring the link. \$\endgroup\$ – MichaelHouse Nov 2 '14 at 16:23
  • \$\begingroup\$ Added a quote so you you can see it better \$\endgroup\$ – user3797758 Nov 2 '14 at 16:35
  • 1
    \$\begingroup\$ That question on the unity QA site is asked by the same person who asked this question here. The questions are not the same. \$\endgroup\$ – Lasse Nov 2 '14 at 16:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.