Unity API
May 29, 2015 ยท View on GitHub
Get your Unity application to communicate with a Python client.
Setup
Install Unity plugin
- In your Unity project, import
unity-plugin/API.unitypackage. - Drag the prefab from
Assets/Resources/APIinto the Hierarchy.
Install Python client
cd unity-clientpython setup.py install
Run example
- 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.
Usage
Sending data out of Unity into your Python client
Wherever you have data in your scripts to send, call API.instance.SetOutput. For example:
API.instance.SetOutput("position.x", transform.position.x);
Receiving data into Unity from Python client
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");
Using Python client
- Import Fetcher:
from unity_client.fetcher import Fetcher - Make an instance of
Fetcher:fetcher = Fetcher() - In a loop, call
fetcher.sync()
Sending data out of your Python client into Unity
You can write to the inputData dictionary on your Fetcher instance. For example:
fetcher.inputData["force"] = force
Receiving data into your Python client from Unity
fetcher.sync() returns an outputData dictionary that contains data sent out from Unity.
Parameters
Some useful parameters you can adjust:
- On the
APIprefab: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)
Contributors
Updating the API.unitypackage file after making changes
- Open
unity-plugin/srcas 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.unitypackagefile.