Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Diagnostics;
- using System.IO;
- public class RunBat : MonoBehaviour
- {
- protected static string SCRIPT_DEFAULT = "the_script.bat";
- public string EDITOR_BAT_FLAG = " \"EDITOR\"";
- public string EDITOR_PATH = "/../Build/";
- public string BUILD_PATH = "/";
- public string fileName = SCRIPT_DEFAULT;
- public string arguments = "";
- // allows an easy way to trigger the callout in the editor
- public bool testBat = false;
- // 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;
- public ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden;
- Process runningProcess = null;
- Process process = new Process();
- public enum LogLevel
- {
- None,
- Normal
- }
- public LogLevel logLevel = LogLevel.None;
- public virtual void Execute(string fileName)
- {
- // If window is hidden, this has to be specified for the window to actually be hidden in C#
- if (windowStyle == ProcessWindowStyle.Hidden)
- {
- process.StartInfo.CreateNoWindow = true;
- }
- else
- {
- process.StartInfo.CreateNoWindow = false;
- }
- if (process != null)
- {
- process.OutputDataReceived -= ConsumeData;
- }
- //Conditional used in this component to help get us to right location if we're in Editor or not
- #if UNITY_EDITOR
- process.StartInfo.FileName = Application.dataPath + EDITOR_PATH + fileName;
- #else
- process.StartInfo.FileName = Application.dataPath + BUILD_PATH + fileName;
- #endif
- if (logLevel == LogLevel.Normal)
- {
- UnityEngine.Debug.Log("process.StartInfo.FileName: " + process.StartInfo.FileName);
- }
- //Conditional used in batch file to help with pathing differences.
- #if UNITY_EDITOR
- process.StartInfo.Arguments = arguments + EDITOR_BAT_FLAG;
- #else
- process.StartInfo.Arguments = arguments;
- #endif
- if (logLevel == LogLevel.Normal)
- {
- UnityEngine.Debug.Log("process.StartInfo.Arguments: " + process.StartInfo.Arguments);
- }
- process.StartInfo.WindowStyle = windowStyle;
- process.OutputDataReceived += ConsumeData;
- process.Start();
- }
- public virtual void Execute()
- {
- Execute(fileName);
- }
- protected virtual void Update()
- {
- if (Input.GetKeyDown(KeyCode.B) || testBat)
- {
- testBat = false;
- Execute();
- }
- }
- protected virtual void ConsumeData(object sendingProcess,
- DataReceivedEventArgs outLine)
- {
- if (!string.IsNullOrEmpty(outLine.Data))
- UnityEngine.Debug.Log(outLine.Data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement