Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Arnav_Sanghavi, Apr 6, 2019.

  1. Arnav_Sanghavi

    Arnav_Sanghavi

    Joined:
    Feb 2, 2019
    Posts:
    7
    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.
    Code (CSharp):
    1. void Start()
    2.     {
    3.  
    4.         startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    5.         startInfo.FileName = "cmd.exe";
    6.         startInfo.Arguments = "/c python " + @"//File Name";
    7.         process.StartInfo = startInfo;
    8.         process.StartInfo.UseShellExecute = false;
    9.         process.StartInfo.RedirectStandardOutput = true;
    10.         process.Start();
    11.        
    12.      
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Debug.Log(process.StandardOutput.ReadToEnd());
    18.     }
    19.  
    I need to run the game and the program simultaneously & Get the Values Every frame.
    Can Someone Help Thanks.
     
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    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
     
    Arnav_Sanghavi likes this.
  3. Arnav_Sanghavi

    Arnav_Sanghavi

    Joined:
    Feb 2, 2019
    Posts:
    7
    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.
     
    Last edited: Apr 6, 2019
  4. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    I got the info I needed to build my project from https://grpc.io/
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
  6. Eldamir88

    Eldamir88

    Joined:
    Jul 2, 2019
    Posts:
    12
    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 :)

    If anyone feels inspired to share ideas for a better approach, please let me know ;) IMG_0735.jpg IMG_0739.jpg