{"id":17987366,"url":"https://github.com/offchan42/unity3d-python-communication","last_synced_at":"2025-04-05T22:06:35.834Z","repository":{"id":44687032,"uuid":"155156582","full_name":"offchan42/Unity3D-Python-Communication","owner":"offchan42","description":":zap: A very fast, simple, and general inter-process communication example between Unity3D C# and Python, using ZeroMQ","archived":false,"fork":false,"pushed_at":"2024-03-02T00:00:02.000Z","size":886,"stargazers_count":364,"open_issues_count":0,"forks_count":59,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-29T20:09:49.792Z","etag":null,"topics":["csharp","netmq","python","pyzmq","unity3d","zeromq"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/offchan42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-10-29T05:27:43.000Z","updated_at":"2025-02-19T07:25:54.000Z","dependencies_parsed_at":"2024-03-02T00:49:14.691Z","dependency_job_id":null,"html_url":"https://github.com/offchan42/Unity3D-Python-Communication","commit_stats":null,"previous_names":["offchan42/unity3d-python-communication","off99555/unity3d-python-communication"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offchan42%2FUnity3D-Python-Communication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offchan42%2FUnity3D-Python-Communication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offchan42%2FUnity3D-Python-Communication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/offchan42%2FUnity3D-Python-Communication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/offchan42","download_url":"https://codeload.github.com/offchan42/Unity3D-Python-Communication/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247406088,"owners_count":20933803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","netmq","python","pyzmq","unity3d","zeromq"],"created_at":"2024-10-29T19:08:37.291Z","updated_at":"2025-04-05T22:06:35.807Z","avatar_url":"https://github.com/offchan42.png","language":"C#","readme":"# Unity3D-Python-Communication\n\n⚡️ A very fast, simple, and general inter-process communication example between Unity3D C# and Python, using ZeroMQ.\n\n![unity-cmd-play-example.gif](img/unity-cmd-play-example.gif)\nPS. It looks slow in the GIF above because I put a delay of one second between each message so that you can see it\nworking.\n\n## Core Pillars\n\n- **very fast** — ZeroMQ is a networking library that allows you to send huge amount of data from server to client in a short period of time. I'm talking about casually sending/receiving 10,000 requests per second.\n- **simple** — You don't have to explicitly open and bind a socket or know anything about low-level networking.\n- **general** — You can use this to send/receive any kind of data request. You can send image, video, text, JSON, file, or whatever you want. In this example, we are sending text.\n- **inter-process** — The communication is done inside the same machine. Which means very low-latency.\n\n## Introduction\n\n* Have you ever tried to communicate C# code in Unity3D with Python before but could not find a satisfying solution?\n* Have you ever tried implementing communication protocol using file read/write and found out that it's a stupid approach?\n* Have you ever tried communicating using Web HTTP request and found out that it's stupidly slow and high latency?\n* Have you ever tried communicating using socket/TCP/UDP stuff, but it feels like you are reinventing the wheel and you\n  are becoming a network engineer?\n* Have you ever tried to communicate by emulating a serial port, and found out that it's not how cool guys do work?\n* Have you ever tried to send Unity input to python and do some scientific work (maybe even machine learning task)\n  and return the output to Unity?\n* Have you ever tried to build a `.dll` from python or even rewrite everything in `C#` because you don't know how to\n  communicate between python and C# processes?\n* Have you ever tried to embed `IronPython` or `Python.NET` inside Unity but it doesn't allow you to install your\n  amazing external python libraries? (And its minimal power is pretty ridiculous compared to your external python)\n* Have you ever tried to export a `TensorFlow Protobuf Graph` (Deep learning model) and use `TensorFlowSharp` or\n  `OpenCVForUnity` to import the graph inside Unity because you want to use the model to predict stuff in Unity, but it\n  doesn't allow you to use/utilize your new NVIDIA GeForce RTX 2080Ti, and it's also hard to code?\n* Tried `MLAgents`, anyone?\n\nIf you answer **Yes** to any of these questions but it seems you have found no solutions,\nthen this repository is definitely for you!\n(If you answered **Yes to all** questions, you and me are brothers! 😏)\n\nI've tried a lot. With a lot of searching on the internet, I've found no solutions that is simple, fast, and general\nenough that I can apply to any kind of communication between Python and Unity3D. All I've done in the past were simply\na hack to either get my scientific computation work in Unity instead of python, or communicate between the processes painfully.\n\n_Until I found ZeroMQ approach from this [repository](https://github.com/valkjsaaa/Unity-ZeroMQ-Example)\n(and some head scratching)._\n\n## Solution Explanation\n\nI've built a `request-reply` pattern of ZeroMQ where Python (server) replies whenever Unity (client) requests\na service from Python.\n\nThe idea is to create a separate thread inside Unity that will send a request to python, receive a reply and log the reply\nto the console.\n\n## Getting Started\n\n1. Clone this repository using `git clone https://github.com/offchan42/Unity3D-Python-Communication.git` command.\n2. Open UnityProject (its `dll` files are targeting .NET 4.x version) and run `Assets/NetMQExample/Scenes/SampleScene`.\n3. Run python file `PythonFiles/server.py` using command `python server.py` on a command prompt.\n4. You should start seeing messages being logged inside Unity and the command prompt.\n\nSpecifically, Unity will send request with a message `Hello` 10 times, and Python will simply reply `World` 10 times.\nThere is a one second sleep between each reply on the server (to simulate long processing time of the request).\n\nPlease read the comments inside `PythonFiles/server.py` and `UnityProject/Assets/NetMQExample/Scripts/` and you will\nunderstand everything more deeply.\n\n**The most important thing is that you should follow the 4 getting started steps first. Don't skip it!** ❣️\n\nAfter you've understood most of the stuff but it's not advanced enough, you should consult the official\n[ØMQ - The Guide](http://zguide.zeromq.org/page:all).\n\n## Requirements\n\n* [PyZMQ](https://pyzmq.readthedocs.io/en/latest/) is the Python bindings for [ZeroMQ]((http://zeromq.org/)). You can install it using\n  `pip install pyzmq` command or see more installation options [here](http://zeromq.org/bindings:python) or\n  [here](https://github.com/zeromq/pyzmq).\n* [NetMQ](https://netmq.readthedocs.io/en/latest/) is a native C# port of ZeroMQ. Normally you need to install this using\n  `NuGet` package manager inside `Visual Studio` when you want to build a .NET application, or you could install using\n  `.NET CLI`. But for this repository here, you don't need to do any of the installation because we've already included\n  `AsyncIO.dll` and `NetMQ.dll` for you inside `UnityProject/Assets/NetMQExample/Plugins/` directory.\n  If you want to build your own `dll` files, please take a look at\n  [this issue](https://github.com/valkjsaaa/Unity-ZeroMQ-Example/issues/7).\n\n## Known Issues\nBased on [this issue](https://github.com/zeromq/netmq/issues/631), the NetMQ implementation is not working nicely with Unity. If you create more than one ZeroMQ client in Unity, the Unity editor will freeze.\n\n## Troubleshooting\n\n* While both server and client are running and communicating fine, I kill the server process, restart the server, then both server and client seem to not be communicating anymore. Why don't they continue communicating? Is this a bug?\n\n  No, this is the expected behavior of ZeroMQ because of the simplicity of the code. It's mentioned in the guide\n  [here](http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive). If you want to make the code better, which is not\n  the focus of this example, you can learn more about ZeroMQ as suggested in the screenshot below.\n  ![troubleshooting-1.PNG](img/troubleshooting-1.PNG)\n  \n  The problem is that when you restart the server, the server won't reconnect to the old client anymore. You have to restart the client also.\n\n## Disclaimer\n\nThis repository is designed to be a **minimal learning resource** for getting started. It's not a fully working high-level package.\nAfter you understand the example, my job is done.\n\nMost of the code are just copies from the official ZeroMQ tutorial. I try to make this as simple to grasp as possible,\nso I only log the message to the console and nothing fancy. This is to minimize the unnecessary learning curve.\n\n## TODO\n\n* [ ] Add a complicated example of how to use it for real\n* [ ] Show how to do this with SocketIO. SocketIO is another approach I found very viable and stable. I use `BestHTTP` package in Unity for SocketIO client and use `python-socketio` as SocketIO server. And it does not have the issue of making Unity editor freezes.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffchan42%2Funity3d-python-communication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foffchan42%2Funity3d-python-communication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffchan42%2Funity3d-python-communication/lists"}