Cost of interop in Unity?

Hello everyone,

I am curious about the internals of scripting in Unity.

For my last project, we embedded Lua into C to use as a scripting language. Calling a C function from inside of Lua had a serious performance cost.

What is the performance overhead of calling Unity APIs from inside of a C# script? Is this the sort of thing I should be doing inside of a tight loop?

I see a lot of demos (for example, the Space Shooter project) where the authors attach copies of scripts under objects to give them properties like constant velocity. In a world where interop is expensive, one might instead have a single script that manages newtonian motion for a large collection of objects. This is annoying to do because the logic is typically more complicated.

I was wondering if maybe because Unity is using C# that there is no interop cost at all - maybe my scripts are compiled to native code and directly thunked into the Unity internal code at build time?

Sorry, but you first talk of interop between C and LUA, then switch to asking about performance of having multiple object with each their behaviour versus one handling the behaviour of many.

Or are you talking about something else?

Scripts within Unity are not compiled into native code, in Unity’s case, that would be C++. They claimed they would offer that feature in Unity 5. For now, C#/JS/Boo are bytecoded and ran in their own environment.

The cost of communication between C++ and C# is almost negligible, in most cases. Also, most loop are trigger by the C++, but ran and looped by the C#… So there’s no boxing overhead for having more objects being updated.

I think the question was, is it a bad idea to have a function call to C++ land in a loop in C# land?