2

Now, I have resource-consuming C# script, uses Unity API. If I try to write external library (due to performance reasons), can I access Unity3D API from C/C++ code (e.g. Physics.OverlapSphere or Physics.SphereCast functions)?

1 Answer 1

3

No, you cannot. The Unity3d classes are only accessible to C# code. C++ plugins cannot call Unity3d functions. The only exception are rendering plugins, as described in here.

As a aside: The Physics functions are likely heavily optimized in the Unity engine, so if the main cost of your resource-consuming script comes from the usage of these functions, the gain would be minimal or even negative due to the need of marshalling the data for passing them to the C++ plugin and back. You can check this using the script profiler is you have the Pro version.

As another aside, you will likely get faster and better answers on https://answers.unity3d.com/, the dedicated Q&A site for Unity.

Sign up to request clarification or add additional context in comments.

6 Comments

@andreas, my script will use Physics.* functions inside of for-loop, with up to 100K iterations. Hmm, now I'll try to optimize my c# code.
If you're calling those functions 100k times per frame you might find yourself in difficulty: that's 3 million calls per second at 30fps.
Dan, this is true. lewis, in this case, you should really try and see if there are other, less computationally intensive ways of achieving what you want.
@DanPuzey I'm try to realize some cell-automation algorythm, and external influence tp it's cells. Cell automata implemented in plane's mesh, and quads (triangle pairs) are cells. Influence area implemented by Cylinder object's radius. Now, I need to find all cells than affected by influence (in Unity - plane's mesh triangles, overlaps by Cylinder).
@lewis, do you really need to repeat the calculation of the influence area each frame? If not, you could do this once and save the results for the following calculations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.