Sharing data between unity and python

I’m trying to send the status of a game object from unity to python. Therefore I create a text file in unity and insert my data into it and then I try to open and use the text file in my python file(this procedure goes on as the game runs). Obviously, I have a problem sharing this text file. Is there a better way to send data from unity to python? (In order to avoid streaming problems)

Thanks :slight_smile:

Files that are in your Unity editor, like textures, sound files, text files and so on are embedded in the Unity resources, thus not possible to access from another process (at least not easily). Moreover, they cannot be modified by the game once compiled.

If you want to share your data using a text file, you must create it using C#, with the [standard .NET way][1], in a given path that your Python script would know.

But this is clearly not what you want here, since the status of the game will be always changing. If you share your data with the text file, you will likely run into issues like Python reading while you are writing it from Unity.

There are several ways to share data between processes:

  • Sockets, like you would do to share your data with a distant computer. The sole difference here is that the server and the client will be on the same machine. It’s up to you to choose whether Unity is the server or the client, it depends on how your things work. See here.
  • Another way is to use anonymous pipes. These are well documented for the C# side, but not really on the Python side when it comes to Windows. See this if you are under Windows and want to use anonymous pipes.
  • Host your Python directly from Unity using a library such as IronPython, which allows a lot of interoperability between the host (C#) and the hosted scripts (your Python CNN).

Regards

I’ve gone through a lot of trouble regarding this Unity-Python communication issue. I’ve tried a lot and eventually settled down to ZeroMQ approach.

See Getting Started section and clone my repository here. You are going to like it

This might be useful to others in the future.
Have a look at this repo.

I know i am a bit late but i wanted to share my thought for anyone strolling by this post just like i did.

While the options above are complicated a more simple way is possible, if you do not have to much data to handle.
If you were to write the info you got with python to a Json file you could read this with unity.