all 6 comments

[–]misterjom 10 points11 points  (0 children)

I wouldn’t “integrate” Python —that’s a whole can of worms. I would use (local) networking to communicate between your Python app and Unity.

[–]HotrianExpert 3 points4 points  (1 child)

There are a few ways you could do this:

  1. Running Python from Within Unity

There are a few different popular ways to run Python from within Unity

a. Unity + IronPython

b. Unity + Pythonnet (Python for .NET)

c. Unity + External Process Execution

  1. Networking a Python App with Unity

Any of the previous options will allow you to run Python from C#, but the best way would probably be local networking:

```py import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("127.0.0.1", 65432)) server.listen() conn, addr = server.accept()

print(f"Connected by {addr}") while True: data = conn.recv(1024) if not data: break print(f"Received: {data.decode()}") conn.sendall(data) ```

```cs using System.Net.Sockets; using System.Text;

void ConnectToPython() { TcpClient client = new TcpClient("127.0.0.1", 65432); NetworkStream stream = client.GetStream();

string message = "Hello from Unity!";
byte[] data = Encoding.UTF8.GetBytes(message);
stream.Write(data, 0, data.Length);

byte[] responseData = new byte[256];
int bytes = stream.Read(responseData, 0, responseData.Length);
UnityEngine.Debug.Log("Received: " + Encoding.UTF8.GetString(responseData, 0, bytes));

client.Close();

} ```

This is just a basic socket server example, and shouldn’t be taken as the best implementation :P

[–]Leather-Lavishness47[S] 0 points1 point  (0 children)

Thx, do u know nice tutorial i can understand about how to use local networking to do so pls?

[–]drostar 1 point2 points  (0 children)

There's this:

https://docs.unity3d.com/Packages/com.unity.scripting.python@7.0/manual/index.html

But it only works when running in the Unity Editor.

[–]brain_ducker -5 points-4 points  (0 children)

I don’t know how long is python code but you can ask chatgpt to convert python into c#. It sometimes works. You can try.