- Home /
Passing a value from Unity to Objective C
Hi, from the title I'm trying to pass a value in this case a file from using Application.CaptureScreenshot which is a string type. The capture screen function is in a js script so I pass the value to cs code. Here's what I have so far:
//saveToAlbum.cs
private CaptureScreen captureScreen;
[DllImport("__Import")]
extern static public string savePicture(string picture);
private string temp;
void Awake() {
//Get the JavaScript component
captureScreen = this.GetComponent<CaptureScreen>();
}
void Update() {
if(captureScreen.press == true) {
//print(captureScreen.picName);
temp = savePicture(captureScreen.picName);
}
}
It builds fine but now the tricky part for me in calling it in Objective C. So I wrote this:
// MyApplication.h
extern "C" {
NSString *savePicture(NSString *picture);
}
and
// MyApplication.mm
NSString *savePicture(NSString *picture) {
NSLog(@ "@", picture);
return picture;
}
At the moment, this is just to test printing out the names of the pictures. Whenever i run this, xcode crashes and has this in the output console "Unable to resolve pinvoke method"
What am I doing wrong or is there a better way of doing this? If this works I'm hoping to use UIImageWriteToSavedPhotosAlbum.
Appreciate the help, thanks very much.
Answer by DeveshPandey · Jan 25, 2013 at 05:15 AM
Hi Hakimo,
Remember me?
Anyway the problem is, you call a objective c function by passing a string, but I can see you don't change the received string to NSString.
Use this function to change your string to NSString
NSString* CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
}
and modify your function as:
extern "C" {
void savePicture(const char* picture)
{
NSLog(@"Hi i am in objective c---",CreateNSString(picture));
}
}
Note : Save this file with .mm extention
Dear Hakimo, please see my previous posts then you get the answer why x-code is going to crash. The reason is native function are only worked on actual device not on the simulator or etc.
See this : http://docs.unity3d.com/Documentation/$$anonymous$$anual/PluginsForIOS.html
Using Your Plugin from C# : here is the answer
Hi Devesh, apparently I've made a huge arse of myself. I just noticed what was giving me the errors and it's one of the first few lines. I placed "[DllImport("__Import")]" ins$$anonymous$$d "[DllImport("__Internal")]". I guess my $$anonymous$$d was someplace else when i was writing it. It now prints "@"Hi i am in objective c---"" but it's not printing the string values from unity. Is there something I should add in my saveToAlbum.cs file? Thanks again.
Hakimo,I faced same error and the solution is :
You have to make a communication layer in objective c which will be accessed from C#, and save it with .mm extension(You have already done).
$$anonymous$$ake a separate class with .m extension which will transfer your photo from given URL. You have to call function of .m file from .mm file.
Flow is given below -
// class CommunicationLayer.mm
extern "C" {
void savePicture(const char* picture) {
call function savePhoto(CreateNSString(picture)) of TransferImage class
}
}
// TransferImage.m (also have .h file)
in this class you have to implement your whole logic.
i.e.
savePhoto()
didFinishSavingWithError()
I think you have an idea to do that. If you have still facing any problem then you can contact me.
As I told you earlier, you have write code in Objective -C to transfer image and save it as .mm file. Your flow is correct, I don't know objective-C therefore I can't help you in Objective-C.
Now your flow and everything is correct, you have to debug your code by yourself. $$anonymous$$ake sure your image will stored in Document directory, and you have called objective C code after image saved in Document directory and all possibilities. :)