- Home /
How to read the console values in Unity?
I am creating a windows application which will allow the user to upload the file and then access that file in unity application. Basically It's a window for selecting images presented on the screen and there is a option to upload your custom image as well. For uploading the custom image I am using a node script which will direct the user to a local webpage from where he/she can upload the value and the location of that image will be given the unity application for using it.
So far I have got to run the local node js server using bat file. But the issue not is the server returns the location of file which is given to console, how can I access that value which is returned into unity.
Here is the part of C# code that i am using to execute the bat file. ==> "node upload_server.js" is command for starting the local server for uploading the file.
public void CreateBatchFile()
{
if (!System.IO.File.Exists (batFilePath)) {
StreamWriter textSr = File.CreateText (batFilePath);
textSr.WriteLine ("@Echo off");
textSr.WriteLine ("echo \"starting local server\"");
textSr.WriteLine ("cd c:\\users\\shubh\\Desktop\\MV_test\\upload-server\\");
textSr.WriteLine ("node upload_server.js");
textSr.Close ();
} else {
UnityEngine.Debug.Log ("bat file already exists");
}
}
public void RunLocalServer()
{
try {
Process myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = batFilePath;
myProcess.EnableRaisingEvents = true;
myProcess.Start();
myProcess.WaitForExit();
int uploadedFileLoc = myProcess.ExitCode;
print(uploadedFileLoc);
} catch (Exception e){
print(e);
}
}
Please help!!
Answer by enigmatic · Dec 27, 2016 at 12:32 PM
Yea! got it quickly. I'll post here my solution if anyone else may need it someday in future.
I just have to add the RedirectStandardOutput to true, then set enableRaisingEvent to true and finally read the output after the execution is finished by ReadToEnd() from standardOutput.
myProcess.StartInfo.RedirectStandardOutput=true;
myProcess.EnableRaisingEvents = true;
myProcess.Start();
//myProcess.BeginOutputReadLine();
myProcess.WaitForExit();
tempFilePath=myProcess.StandardOutput.ReadToEnd();
the tempfilepath will contains everything from your console output. :D