Advertisement
burtonposey

RunBat - run batch files from Unity!

Mar 10th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. public class RunBat : MonoBehaviour
  7. {
  8.     protected static string SCRIPT_DEFAULT = "the_script.bat";
  9.  
  10.     public string EDITOR_BAT_FLAG = " \"EDITOR\"";
  11.  
  12.     public string EDITOR_PATH = "/../Build/";
  13.     public string BUILD_PATH = "/";
  14.  
  15.     public string fileName = SCRIPT_DEFAULT;
  16.  
  17.     public string arguments = "";
  18.  
  19.     // allows an easy way to trigger the callout in the editor
  20.     public bool testBat = false;
  21.  
  22.     // determines whether or not the process will open a visible console window. Note: If you use ProcessWindowStyle.Hidden, you must specify process.StartInfo.CreateNewWindow = true;
  23.     public ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden;
  24.  
  25.     Process runningProcess = null;
  26.     Process process = new Process();
  27.  
  28.     public enum LogLevel
  29.     {
  30.         None,
  31.         Normal
  32.     }
  33.  
  34.     public LogLevel logLevel = LogLevel.None;
  35.  
  36.     public virtual void Execute(string fileName)
  37.     {
  38.         // If window is hidden, this has to be specified for the window to actually be hidden in C#
  39.         if (windowStyle == ProcessWindowStyle.Hidden)
  40.         {
  41.             process.StartInfo.CreateNoWindow = true;
  42.         }
  43.         else
  44.         {
  45.             process.StartInfo.CreateNoWindow = false;
  46.         }
  47.  
  48.         if (process != null)
  49.         {
  50.             process.OutputDataReceived -= ConsumeData;
  51.         }
  52.  
  53.         //Conditional used in this component to help get us to right location if we're in Editor or not
  54. #if UNITY_EDITOR
  55.         process.StartInfo.FileName = Application.dataPath + EDITOR_PATH + fileName;
  56. #else
  57.         process.StartInfo.FileName = Application.dataPath + BUILD_PATH + fileName;
  58. #endif
  59.         if (logLevel == LogLevel.Normal)
  60.         {
  61.             UnityEngine.Debug.Log("process.StartInfo.FileName: " + process.StartInfo.FileName);
  62.         }
  63.  
  64.         //Conditional used in batch file to help with pathing differences.
  65. #if UNITY_EDITOR
  66.         process.StartInfo.Arguments = arguments + EDITOR_BAT_FLAG;
  67. #else
  68.         process.StartInfo.Arguments = arguments;
  69. #endif
  70.  
  71.         if (logLevel == LogLevel.Normal)
  72.         {
  73.             UnityEngine.Debug.Log("process.StartInfo.Arguments: " + process.StartInfo.Arguments);
  74.         }
  75.  
  76.         process.StartInfo.WindowStyle = windowStyle;
  77.  
  78.         process.OutputDataReceived += ConsumeData;
  79.  
  80.         process.Start();
  81.     }
  82.  
  83.     public virtual void Execute()
  84.     {
  85.         Execute(fileName);
  86.     }
  87.  
  88.     protected virtual void Update()
  89.     {
  90.         if (Input.GetKeyDown(KeyCode.B) || testBat)
  91.         {
  92.             testBat = false;
  93.             Execute();
  94.         }
  95.     }
  96.  
  97.     protected virtual void ConsumeData(object sendingProcess,
  98.             DataReceivedEventArgs outLine)
  99.     {
  100.         if (!string.IsNullOrEmpty(outLine.Data))
  101.             UnityEngine.Debug.Log(outLine.Data);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement