- Home /
Using an Objective-c framework directly in Unity 3d
Hi,
Here's what I have done: 1- Define some functions within a framework In .h :
#import <UIKit/UIKit.h>
FOUNDATION_EXPORT double TestVersionNumber;
FOUNDATION_EXPORT const unsigned char TestVersionString[];
@interface MyLib : NSObject
+ (id)sharedInstance;
- (void)openWebView:(NSString *)url;
- (BOOL)testMethod;
@end
In .m :
#import <Foundation/Foundation.h>
#import "MyLib.h"
#import "ViewController.h"
@interface MyLib()
@property (nonatomic, retain) ViewController *webViewController;
@end
@implementation MyLib
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+ (id)sharedInstance {
static dispatch_once_t pred = 0;
static id instance = nil;
dispatch_once(&pred, ^{
instance = [[self alloc] init];
});
return instance;
}
- (BOOL)testMethod {
NSLog(@"testMethod was called");
return TRUE;
}
@end
3- I then copied the framework to 'Assets/Plugins/iOS/` folder
4- I attached this to the camera in the scene:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class Ttest : MonoBehaviour {
[DllImport ("__Internal")]
private static extern bool testMethod();
// Use this for initialization
void Start () {
var result = testMethod();
if (result) {
Camera.main.backgroundColor = Color.yellow;
}
}
}
4- Built the unity project and ran in the simulator.
I get an error saying testMethod() was not found.
What's the problem? Thanks.
Answer by Aram-Azhari · Jun 12, 2015 at 03:10 PM
UPDATE: Ok, after further research I realized that the framework itself needs to be in a specific manner to work.
To work on the device, in the framework project:
Build Settings -> MachO -> The framework should be marked as “Static Library”
Build Settings -> Turn off “Build active architecture only”
Build Settings -> Other Linker Flags should be ‘-ObjC’
Thank you @superdev for this. Unfortunately the .m file that I have created doesn't work properly with the framework that i created. So here's what i've done but no success:
Create a framework with the name MyLib.framework
Copy the framework and the .h/.m wrapper files to plugin/iOS/
Call the wrapper function _testMethod from the c# code (added the dllimport signature too).
I get errors regarding ViewController that lives in the framework. I commented the framework code out for now just to test a simple method that is in the mention .m file.
I got this error:
EntryPointNotFoundException: _testMethod at (wrapper managed-to-native) iosCallerTest:_testMethod () at iosCallerTest.Start () [0x00000] in :0
Any suggestions?
Answer by superdev · Jun 12, 2015 at 09:50 AM
Adding my comment from here.
Basically, you have to have the methods you want to call exposed directly in a header or .m file.
You would have another "outside" object store your shared instance and hold reference to your class so that will call the MyLib functions.
You won't be able to call them directly from the C# side.
You can have the sharedInstance call whatever other functions you want to call within the static library or framework, but they don't necessarily need to be 1 to 1 unless you need to call each function within the MyLib class separately.
Answer by Cris-Uy · Jun 28, 2017 at 12:46 AM
https://medium.com/@rolir00li/integrating-native-ios-code-into-unity-e844a6131c21
You can follow this thread. :)