Getting values from a Pyton Script everyframe into a Unity Script.

Hi, I have been trying to get values that get printed in CMD from a python script into Unity. I have been able to open the application via c# but the issue i have been facing is that when the python program is executed unity waits for program to finish and then runs the game.

This the Code i am using to get Input Into Unity.

void Start()
    {

        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/c python " + @"//File Name";
        process.StartInfo = startInfo;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();
       
      
    }

    void Update()
    {
        Debug.Log(process.StandardOutput.ReadToEnd());
    }

I need to run the game and the program simultaneously & Get the Values Every frame.
Can Someone Help Thanks.

That could be done with gRPC. I’ve used it for a Unity project that continuously exchanges data with an exe file. In my case, the exe is a C# program, but gRPC also has a Python API.
https://github.com/mbaske/yolo-unity

1 Like

Hi, thanks Will Look Into it.

Edit: Looked into it. Found it quite Interesting. Is there any good Tutorial or something u can guide me towards? Will be a Great Help. Thanks.

I got the info I needed to build my project from https://grpc.io/

If you feel more adventurous you can play with web sockets too, here is an article for explanation (it’s not mine): Making 3D Interfaces for Python with Unity3D | HackerNoon

Old post, but thank you for your inputs all of you. I’m building my own little Raspberry pi console, and have done some PyGame, but would like to use Unity instead. There are two main problems for me, which I now have possible solutions for:

  • How to build for the pi? → Just do WebGl for now
  • How to access GPIO and custom peripherals from Unity game? → Have a Python script read GPIO and make available through web socket, let the WebGl build read input from the web sockets.

Just have to test out how well it holds together. As long as I pass the web socket data through the loopback interface, there shouldn’t be any notable input lag (at least for the types of games I’m looking at :slight_smile:

If anyone feels inspired to share ideas for a better approach, please let me know :wink: