How to call a Unity/Mono script function from native code?

So I’ve got a script calling functions in native code, but I need to do the reverse. I’m wondering how to pass a callback function to native code that will get called when an event in native code occurs.

I’m talking about using the plug-in system in Unity iPhone Advanced.

you can not call unity from xcode, only vice versa.
Unity is not a framework to use within your app, it is the app and offers you now the capabilities to expand it with native functionality.

Let me confirm the answer above - dreamora is right

Dreamora, I know what Unity is.

What I’m talking about is calling a callback function from Xcode.

For example: From Unity I call a native function that will take a few frames to provide results (because it retrieves data from the web). I’d like to register a C# function that can be called from native code when the results are ready.

The only other way to do this would to be have a native function that gets polled from C# to see if the results are ready, which isn’t as elegant.

It is definitely possible to pass function pointers from mono to native code, but I’m wondering if it can be done in Unity iPhone.

Your answer doesn’t contain any technical details about why it’s not possible, so I’m not sure if you understood what I’m asking.

This is almost exactly what I’m trying to do:
http://netcode.ru/dotnet/?lang=&katID=30&skatID=269&artID=7399

If this isn’t possible with Unity iPhone, I’d like to request a UnitySendMessage() function that can be called from ObjC. This would basically be identical to the way the external SendMessage function works in the Unity Web Player.

http://unity3d.com/support/documentation/Manual/Unity%20Web%20Player%20and%20browser%20communication.html

Just create some kind of messaging queue that your XCode stuff could put data into when it’s done and your C# code can poll or something.

Just a random thought.

Yeah, polling will work, but it would be nice to have a more elegant solution in the future.

Found a fairly good explanation of what the problem is:
http://monotouch.net/Documentation/Limitations#Reverse_Callbacks

Unfortunately Unity’s compiler doesn’t have the MonoPInvokeCallbackAttribute flag (yet?).

That should not be no problem.
Send out the delegate to the function when you call the native function and the processing is done it calls that function again and thus should be able to inform unity of it.
But keep in mind that you will likely have some fun with marshaling and the InteropServices doing this.
Should, because that works with plugins in Unity Pro. I’ve not tested it on the iphone yet.

What I though see as a problem is that calls to plugin functions are blocking. so the function itself can not take several frames. Guess you have some kind of management framework around it that will have to store requests with callbacks / delegates.

I hope that helped :slight_smile:

And sorry should the first answer have sounded harsh. Wasn’t its intend. Just attempted to plot the picture of how the technology is layed out in case you had a different picture of it. Saves you time thinking up ways that can not work due to the wrong picture of the whole situation.

Please use our bugreporter to request a feature. We do not track forums for feature requests.

Has anyone gotten the delegate callback to work? Or is anyone at least somewhat knowledgeable with respect to how to get a Mono delegate into Objective C?

I’m trying to enable menu functionality via a plugin, and everything works, up to the callback part. I’d rather not poll, if I can avoid it.

I’m a complete noobie with Objective-C…

according a posting by a Unity Dev, its not possible to use delegates callbacks

That’s too bad.

So polling is the only way, then? Or is there some other way to call a Mono function directly from Objective-C?

EDIT: Okay, so apparently the overhead associated with polling a boolean from Cocoa in Update() is basically nothing.

I just used a quick boolean to say has anything changed or not, and if so, get all the rest of the stuff. Simple.

So is there no way to launch Unity from a cocoa project?
Sorry but i don’t know Objective-C, but i found this:

http://technology.blurst.com/category/iphone/

it’s like using iPhone Advanced integration with Objective-C?

Thank you.