These are encapsulated Python and Unity modules for continuous TCP communication between these.
For Japanese: 【Python, Unity】(ほぼ)コピペでUnity・Pythonの双方向通信を簡単に記述する
Use
PythonSocketfor yourPythonproject,UnitySocketfor yourUnityproject.
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 thenameorjson!inname
The 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 thePythonserver.
This will block untilUnityis connected When data is received, the callback will be invoked. -
UnityConnector.send(string, dict)
Send JSON data toUnity -
UnityConnector.stop_connection()
Stop the connection and send quit code toUnity
Make 3 callbacks:
- data received.
Called when received data from another one.
2 params given: data_format_name(str) and data json(dict) - timeout
Called when the connection failed. No params given - quit
Called whenUnitysent a quit code. No params given
Put
to your Unity project.
Make
- Subclasses of
DataClassfor each of your data formats. [Example] - A subclass of
DataDecoderand just overrideDataToType()[Example]
and attach
PythonConnector- your inherited
DataDecoder
to an UnityObject (anything OK, even a empty class)
You can observe ConnectionTest.cs
-
PythonConnector.instance.RegisterAction(type, function)
Register callback corresponds to thetypetypeisDataClass -
PythonConnector.instance.StartConnection()
Starting connection withPythonCall this afterPythonstarted. -
PythonConnector.instance.Send(string, DataClass)
Send data toPythonGive data format name and dataclass (automatically converted into JSON if you useSerialize) -
PythonConnector.instance.StopConnection()
Send quit code toPythonand stop connection.