- Home /
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 :)
How is your Python code run? Do you host it from Unity with a specialized library, or is it two separate processes? In what is the context do you use that?
They are two separate processes. I gather data from my unity file and feed it to my python file(as input for my convolutional neural network).
Answer by troopy28 · Aug 20, 2018 at 04:01 PM
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, 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
Answer by off99555 · Oct 29, 2018 at 07:54 AM
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 https://github.com/off99555/Unity3D-Python-Communication
Answer by Siliconifier · Dec 25, 2020 at 03:09 AM
This might be useful to others in the future. Have a look at this repo.
Answer by Chrubgamer · Jul 11, 2021 at 02:45 PM
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.
Your answer
Follow this Question
Related Questions
unity and databases 1 Answer
Streaming music 3 Answers
It is possible to start a streaming AudioClip not from the beginning ? 1 Answer
Asset Managing for Mobile Devices 0 Answers
Live Streaming on Android using Unity 0 Answers