These are encapsulated Python and Unity modules for continuous TCP communication between these.
For Japanese: 【Python, Unity】(ほぼ)コピペでUnity・Pythonの双方向通信を簡単に記述する
Use
PythonSocket for your Python project,UnitySocket for your Unity project.PythonSocket is a server and UnitySocket is the client.
Ignite PythonSocket before the UnityScoket
these 2 will send the data to each other in the format below:
<s>{json_format_name}!{json}<e>
Don’t use:
<s> or <e> either in the name or json! in nameThe format decoding will get an error.
If either Python or Unity quits the connection. They will send a quit code end!
pip install "git+https://github.com/konbraphat51/UnityPythonConnectionModules.git#egg=UnityConnector&subdirectory=PythonSocket"
You can observe ManualTester.py for running example.
Use UnityConnector
from UnityConnector import UnityConnector
UnityConnector.start_listening()
Ignite the Python server.
This will block until Unity is connected
When data is received, the callback will be invoked.
UnityConnector.send(string, dict)
Send JSON data to Unity
UnityConnector.stop_connection()
Stop the connection and send quit code to Unity
Make 3 callbacks:
Unity sent a quit code.
No params givenPut
to your Unity project.
Make
DataClass for each of your data formats. [Example]DataDecoder and just override DataToType() [Example]and attach
PythonConnectorDataDecoderto an UnityObject (anything OK, even a empty class)
You can observe ConnectionTest.cs
PythonConnector.instance.RegisterAction(type, function)
Register callback corresponds to the type
type is DataClass
PythonConnector.instance.StartConnection()
Starting connection with Python
Call this after Python started.
PythonConnector.instance.Send(string, DataClass)
Send data to Python
Give data format name and dataclass (automatically converted into JSON if you use Serialize)
PythonConnector.instance.StopConnection()
Send quit code to Python and stop connection.