简体   繁体   中英

MonoTouch C# and Obj-C mixing?

Following on from my last question on optimization I'm finding it easier to believe that it's simply not possible to optimize my code further to get better performance in my game.

So now I have a new idea and would like to know if it's possible or even worthwhile to implement. Since the clear bottleneck in performance is from the physics processing which is based in .net, I was thinking, would it be possible to fit in an objective-c or C++ based physics engine (such as box2D) to interact with Mono?

For example in the Mono code I would do something like CreateBox() but behind the scenes the box is created in Obj-C/C++. Or when I do a physics update, I simply call the function in Mono and the brunt work happens in Obj-C/C++

If this is possible can anyone point me in the right direction to get started?

Yes.

There are two ways to do this, via a C API and an Objective-C API.

With a C API, write your native code in C (or C++ with extern "C" ), compile your code into a static library, link your MonoTouch app against the static library , then use P/Invoke to access your C functions. However , unlike "normal" P/Invoke where you need to specify a library name, you need to specify "__Internal" as the library name:

[DllImport ("__Internal")]
static extern void MethodInStaticLibrary ();

This same technique is used by MonoTouch to call from eg System.dll into the MonoTouch runtime.

Using Objective-C is the same-yet-different. Instead of a static library exporting a C ABI, you need a static library exporting an Objective-C ABI, link against the static library, then provide an API binding for your Objective-C library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM