Search Unity

Protocol to communicate between Python and Unity?

Discussion in 'Scripting' started by schuchmannsebastian, Feb 10, 2020.

  1. schuchmannsebastian

    schuchmannsebastian

    Joined:
    Feb 27, 2019
    Posts:
    10
    Hello!

    I want my Python scripts to communicate with Unity. Any recommendations on how to do it? Socket, HTTP?

    Any help is much appreciated!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    both will work fine, tcp/udp sockets, websockets too.
    are you sending some queries, or streaming data realtime or something else?
    (is that within same desktop, or multiple clients over internet or ?)
     
  3. schuchmannsebastian

    schuchmannsebastian

    Joined:
    Feb 27, 2019
    Posts:
    10
    I just want to send some queries, no streaming of data.
    It will probably be within the same desktop for now but having the python scripts run on a different pc at a later time would be great as they are CPU intensive.
    So when you are talking about TCP/UDP Sockets, are you referring to .net sockets or the unity network stuff. I am a bit confused about what the Unity Network layer can do and what it can't do.

    Thanks! :)
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    if its queries (from unity to python), i'd probably just run http-service on python, and call that with unitywebrequest.

    for tcp/udp can use regular c# networking stuff, nothing unity specific.
     
  5. schuchmannsebastian

    schuchmannsebastian

    Joined:
    Feb 27, 2019
    Posts:
    10
    Thanks for your help so far! So the queries are from Python. This means Unity has to be the server right? I am wondering what's the best way to do it is without blocking the application. Any ideas?
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    then i'd use sockets, not sure about how to even run webservice in unity.

    sockets can be async, so can do all processing in another thread.
     
  7. schuchmannsebastian

    schuchmannsebastian

    Joined:
    Feb 27, 2019
    Posts:
    10
    Okay, I'll do that. I found this Rest Client, which seems to do the job. So in Unity then, I would send a Request to the Server on every frame checking for a new query?
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411
    if you want to query so fast, then i'd especially use sockets
     
  9. schuchmannsebastian

    schuchmannsebastian

    Joined:
    Feb 27, 2019
    Posts:
    10
    Ah okay I see. Thanks you!
     
  10. DitchTurtL

    DitchTurtL

    Joined:
    May 23, 2017
    Posts:
    10
    I can't vouch for it's compatibility, but there are a few posts out there from people trying to get SignalR to work with unity. This would be the most 'correct' way I can think of to implement something like this. Generally, Unity would be the client, but the signalR server would be able to push data down if necessary.

    If you end up going at it barebones like, just keep in mind you'll be streaming the data or offloading the work to another process. The connection process takes far longer to open and close than it does to transfer data on an already open connection.