These are encapsulated Python
and Unity
modules for continuous TCP communication between these.
For Japanese: 【Python, Unity】(ほぼ)コピペでUnity・Pythonの双方向通信を簡単に記述する
Use
PythonSocket
for yourPython
project,UnitySocket
for yourUnity
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 thename
orjson
!
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 thePython
server.
This will block untilUnity
is 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 whenUnity
sent a quit code. No params given
Put
to your Unity
project.
Make
- Subclasses of
DataClass
for each of your data formats. [Example] - A subclass of
DataDecoder
and 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 thetype
type
isDataClass
-
PythonConnector.instance.StartConnection()
Starting connection withPython
Call this afterPython
started. -
PythonConnector.instance.Send(string, DataClass)
Send data toPython
Give data format name and dataclass (automatically converted into JSON if you useSerialize
) -
PythonConnector.instance.StopConnection()
Send quit code toPython
and stop connection.