1

So I did some research on the subject, and it turns out there is such a thing as a Python interpreter for Unity, but I'm only interested in using Python as a quick calculation step in my project.

Long story short, I want to be able to input an array of numbers into a Python script and get an array of numbers back in a coroutine or an Update function.

Performance is key, as I'm planning on doing matrix calculations in Python, using TensorFlow and Numpy. Any way to achieve this and retain at least MOST of its efficiency?

PS. I will also want to later build to an Android / iOS device. Am I asking for too much?

Thanks in advance!

  • 1
    that transcompile is probably going to hurt your performance more than what you gain from the fraction of seconds compared to c# script – Steve Jul 25 '18 at 19:52

1 Answers1

1

I will also want to later build to an Android / iOS device Am I asking for too much?

Yes and because you want to do it with python and also want to support many platforms.

You have to use C++ for this not Python and this is not about language preference. It's about getting the code to run on most platforms supported by Unity since you entioned iOS and Android. Unity supports native plugins written and compiled in C++. Not so much for Python but you can get python to work on Windows. You can't on iOS, Android and WebGL.

TensorFlow API is written in C++ too so write a plugin wrapper with just the functions you need with C++ and you'll be able to run it on almost any platform Unity supports. If you care about performance, it's better that you modify the array you passed to the native function instead of returning new array as a result. For passing array from C# to C++ see this post.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hey! Great answer. I've only got a question about this part of your answer: **it's better that you modify the array you passed to the native function instead of returning new array as a result** What you're saying here is basically that I should use the same array (by reference) in both languages, instead of recreating the object in C++ and then again in C#? –  Jul 25 '18 at 20:21
  • Yes. You don't even have to modify the data you send to C++ if you don't want to. You can also pass another array from C# as parameter to the C++ as a second parameter and that second array will store the result the C++ plugin will give out. Without doing this, you would need to create an array in C++ then create another one in C# then copy the one from C++ to C# then finally implement a C++ function to free the array. Bunch of unnecessary and expensive stuff but I've seen people do that. The goal is to avoid allocating array on the C++ side since you will need to copy and free it manually – Programmer Jul 25 '18 at 20:28
  • Right, that's a good suggestions. Would it matter that I'll end up creating a bunch of other objects in C++ (connected to the actual processes I'll be conducting in the code) as long as I free them within C++ and not try to bring them over to C#? –  Jul 25 '18 at 20:40
  • It doesn't matter and it's normal to create object in C++. Just do it with the `new` keyword but make sure to free it later on otherwise you just added memory leak to your game. Usually, you create the object with and return as as a pointer to C++. Store that pointer to `IntPtr`. When you need to destroy it pass the `IntPtr` to the C++ side that uses the `delete` keyword to free it. See [this](https://stackoverflow.com/questions/40106644/how-to-add-c-code-to-unity-project-easy-way) post for example. – Programmer Jul 25 '18 at 20:52
  • To get started with your plugin see [this](https://stackoverflow.com/questions/49793560/build-c-plugin-for-unity). – Programmer Jul 25 '18 at 20:52