This class encapsulates methods to run Python strings, files andclients inside of Unity.
Currently there is not a way to directly use python within unity. You can use an interpreter that will call functions. This can only take you so far beyond the built in functions that unity currently used. Since you already know Python and probably learned Java in school or have at least seen it. GitHub - Unity-Technologies/pythonnet: Python for.NET is a package that gives Python programmers nearly seamless integration with the.NET Common Language Runtime (CLR) and provides a powerful application scripting tool for.NET developers. A Unity asset extractor for Python based on AssetStudio. Next to extraction it also supports editing Unity assets. So far following obj types can be edited: Texture2D; Sprite(indirectly via linked Texture2D) TextAsset; MonoBehaviour; If you need advice or if you want to talk about (game) data-mining, feel free to join the UnityPy Discord.
Inheritance
PythonRunner
Namespace: UnityEditor.Scripting.Python
Syntax
Fields
PythonRequiredVersion
The Python version we require.
Changing this to 3 isn't going to magically make it work, the constant is just to help find some parts that matter.
Declaration
Field Value
Type | Description |
---|---|
String |
Properties
InProcessPythonVersion
Declaration
Property Value
Type | Description |
---|---|
String | A string representing the version. |
Methods
CallAsyncServiceOnClient(String, String, Object[])
Method to use with C# async/await semantics.Call this if the client is expected to call back the server.If the call to this method is not awaited, the execution of this method willcontinue on its own.
Declaration
Unity Python 3
Parameters
Type | Name | Description |
---|---|---|
String | clientName | name of the client to make the call to. |
String | serviceName | name of the service to be called. |
Object[] | args | the arguments to be passed on to the called service. |
Returns
Type | Description |
---|---|
Task<Object> | The task that is the asynchronous call to the service. Wait for it to finish with Task.wait() or discard the return value if none is expected. |
CallCoroutineServiceOnClient(String, String, Object[])
Convenience function to call a method on a client, asynchronously.Call this if the client is expected to call back the server.
Use as a coroutine in a GameObject:StartCoroutine(PythonRunner.CallCoroutineServiceOnClient('foo', 'bar'))
Or iterate over the enumerator in a Unity coroutine:var pycall = PythonRunner.CallCoroutineServiceOnClient('foo', 'bar');while (pycall.MoveNext()) { yield return null; /* throws here if the result arrives and is an error /}/ do something with pycall.Current if we care about the return value */
This is a wrapper around unity_python.server.server.call_service_on_client_async. If you want keywordarguments, call the Python directly with:
Where args is a tuple or list and kwargs is a dict.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | clientName | The name of the client to call the service on. |
String | serviceName | The name of the service to call. |
Object[] | args | Arguments to be passed to the service. Must be basic types (strings, int, bool) or PyObject. |
Returns
Type | Description |
---|---|
IEnumerator | An IEnumerator. |
CallServiceOnClient(String, String, Object[])
Convenience function to call a service on a client.
This is a wrapper around unity_python.server.server.call_service_on_client. If you want keywordarguments, call the Python directly with:
Where args is a tuple or list and kwargs is a dict.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | clientName | The name of the client to call the service on. Prior to installing MovieCaptioner for Windows, you need to download and install QuickTime 7 and restart your computer to make the QuickTime libraries available to MovieCaptioner. MovieCaptioner for Windows will not launch unless QuickTime is available to it. Download MovieCaptioner for Mac (Mac OSX 10.9 through current Big Sur Mac OSX 11.2.1). Download MovieCaptioner for macOS 10.9 or later and enjoy it on your Mac. MovieCaptioner is the easiest way to create captions and transcripts for your video. Once you load your movie and hit the Start button, it will loop through a few seconds of the movie while you type what you hear. Just load your movie, set your text/background properties, then click the Start button. MovieCaptioner will start playing the first 4 seconds of your video in a loop. Just type what you hear in that loop and then hit your Return/Enter key to record that caption and it's starting timecode. Movie captioner for mac. |
String | serviceName | The name of the service to call. |
Object[] | args | Arguments to be passed to the service. Must be basic types (strings, int, bool) or PyObject. |
Returns
Type | Description |
---|---|
Object | Null if the service returns None (or has no explicit return), else a PyObject. |
CloseClient(String, Boolean)
Closes or reset a client by calling the 'on_server_shutdown' service.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | clientName | The name of the client to close or reset. |
Boolean | inviteRetry | If true, send on_server_shutdown(true). If false, send on_server_shutdown(false) |
EnsureInProcessInitialized()
Ensures the in-process Python API is initialized.
Safe to call frequently.
Throws if there's an installation error.
Declaration
EnsureOutOfProcessInitialized()
Ensures the out of process API is initialized.
Safe to call frequently.
Throws if there's an installation error.
Declaration
ForceRestart()
Reinitialize the server e.g. after a crash.
This is not idempotent.
It will close the server, delete the socket file, then start theserver again.
Does not throw exceptions (but logs them).
Declaration
GetConnectedClients()
Returns the names of the connected clients. If there are multiple instances of a client, returns only one copy of the name.
Declaration
Returns
Type | Description |
---|---|
String[] | An array of string that contains the connected clients. |
GetRPyCVersion()
Declaration
Returns
Type | Description |
---|---|
String | A human-readable string representing the version of RPyC. |
GetSocketPath()
Return the path to the socket file.
Declaration
Returns
Type | Description |
---|---|
String |
IsClientConnected(String)
Declaration
Parameters
Unity Python Plugin
Type | Name | Description |
---|---|---|
String | clientName | The name of the client. |
Returns
Type | Description |
---|---|
Boolean | True if the client is connected, False otherwise. |
NumClientsConnected()
Returns the total number of clients connected to the server.
Declaration
Returns
Type | Description |
---|---|
Int32 |
NumClientsConnected(String)
Returns the number of clients of the same name connected to the server.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | clientName | The name of the client. |
Returns
Type | Description |
---|---|
Int32 | The number of clients connected. |
RunFile(String)
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | pythonFileToExecute | The script to execute. |
RunString(String)
Runs Python code in the Unity process.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | pythonCodeToExecute | The code to execute. |
SpawnClient(String, Boolean, String[])
Spawns a new client by launching a new Python interpreter and having it execute the file.
Returns immediately after spawning the new Python process. If you need Unity to coordinatewith the client, you will need to wait for the client to connect to the Unity server.If the client script fails to run, check the logs to see exactly what was executed andtry to run the script by hand in a shell terminal to find the errors.
The Python interpreter chosen is the one in the Python Settings.
Declaration
Parameters
Type | Name | Description |
---|---|---|
String | file | The file to be executed. |
Boolean | wantLogging | If true, turns on debug logging for the Python client startup. If false, silences all messages and errors during startup. |
String[] | arguments | The arguments to be passed to the script. |
Returns
Type | Description |
---|---|
Object | The Popen Python object that is the newly spawned client. |
StartServer()
Starts the Python server and the job-processing loop. Calling this is idempotent: if the server has already started, this call has no effect.
Declaration
StopServer(Boolean)
Stops the Python server and the job processing loop. Calling this isidempotent: if the server is already closed, this call has no effect.
Declaration
Parameters
Type | Name | Description |
---|---|---|
Boolean | inviteReconnect | If true, signal the clients the server will be restarted. |
WaitForConnection(String, Double)
Waits at most
timeout
seconds for a client to be connected. To beused as a Unity coroutine.Declaration
Parameters
Type | Name | Description |
---|---|---|
String | clientName | The name of the client to wait for. |
Double | timeout | The maximum time to wait on the client, in seconds. |
Returns
Type | Description |
---|---|
IEnumerator | The IEnumerator to iterate upon. Always yields null. |