Get your Unity application to communicate with a Python client.
- In your Unity project, import
unity-plugin/API.unitypackage
. - Drag the prefab from
Assets/Resources/API
into the Hierarchy.
cd unity-client
python setup.py install
- Start proxy server:
python proxy-server/run.py
- (In separate console) Start the example Python client:
python unity-client/examples/echo.py
- Run your Unity application.
You should see data being printed to the example Python client console.
Wherever you have data in your scripts to send, call API.instance.SetOutput
. For example:
API.instance.SetOutput("position.x", transform.position.x);
Wherever you need to read data that you had sent from your Python client, call API.instance.GetInput
. For example:
object force = API.instance.GetInput("force");
- Import Fetcher:
from unity_client.fetcher import Fetcher
- Make an instance of
Fetcher
:fetcher = Fetcher()
- In a loop, call
fetcher.sync()
You can write to the inputData
dictionary on your Fetcher
instance. For example:
fetcher.inputData["force"] = force
fetcher.sync()
returns an outputData
dictionary that contains data sent out from Unity.
Some useful parameters you can adjust:
- On the
API
prefab:Update Rate
- how quickly Unity sends data to the proxy serverRun Speed
- the rate at which time passes in UnityServer URL
- the location of the proxy server (so you can run your application remotely)
- Open
unity-plugin/src
as a project in Unity. - In the menu bar, select
Assets > Export Package
. - Make sure everything is selected, and click
Export
. - Save to replace the
API.unitypackage
file.