Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Run terminal command and get output within Unity application(OSX)

Discussion in 'macOS' started by SweetMei25, May 23, 2019.

  1. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33
    Dear Professionals.
    I would like to run this OSX Terminal command using Unity's Process, ProcessStartInfo and would like to get the output inside Unity application.

    I tried many examples, but all didn't work.

    This is my final code(Not working) and I 'd like any professional help on this script:
    Code (CSharp):
    1. using System;
    2. using System.Diagnostics;
    3. using UnityEngine;
    4. public class GetDeviceInfo: MonoBehaviour
    5. {
    6.     private void Start()
    7.     {
    8. #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
    9.         ExecuteProcessTerminal("ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'");
    10. #endif
    11.     }
    12.     private void ExecuteProcessTerminal(string argument)
    13.     {
    14.         try
    15.         {
    16.             UnityEngine.Debug.Log("============== Start Executing [" + argument + "] ===============");
    17.             ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash")
    18.             {
    19.                 WorkingDirectory = "/",
    20.                 UseShellExecute = false,
    21.                 RedirectStandardOutput = true
    22.             };
    23.             Process myProcess = new Process
    24.             {
    25.                 StartInfo = startInfo
    26.             };
    27.             myProcess.StartInfo.Arguments = argument;
    28.             myProcess.Start();
    29.             string output = myProcess.StandardOutput.ReadToEnd();
    30.             UnityEngine.Debug.Log("Result for [" + argument + "] is : \n" + output);
    31.             myProcess.WaitForExit();
    32.             UnityEngine.Debug.Log("============== End ===============");
    33.         }
    34.         catch (Exception e)
    35.         {
    36.             print(e);
    37.         }
    38.     }
    39. }
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I'm interested in this as well. From past experiments I haven't had much luck getting it to work either. It would be so cool if you could invoke terminal commands from within Unity C#!
     
    SweetMei25 likes this.
  3. SweetMei25

    SweetMei25

    Joined:
    Nov 17, 2018
    Posts:
    33