Socket communication between Unity and Python
There are more and more machine learning tools based on Python, such as Tensorflow.
Many projects use python to train their own models. After training, they then export the graph (.bytes file). Then copy it to unity to load the performance.
Unity's official machine-learning example follows this principle. Unity example github download link: https://github.com/Unity-Technologies/ml-agents
This example shows the communication between unity and python in the most concise code.
The steps are as follows:
Open the unity project and click Run
Enter Python, select main.py, compile and run, and the server socket will enter the listening state.
Click the first button to establish a CS connection. At this time, the server's log can see the fields sent by the client.
Click the second button, and the client continuously sends Hello World to the server.
Click the third button, the server will send a json to the client
Click the fourteenth button and the socket will be disconnected
Notice:
Why is the socket not connected for 30 seconds? The server will actively disconnect