├── README.md.meta ├── Editor.meta ├── Editor ├── ShellHelper.cs.meta ├── ShellHelperTest.cs.meta ├── ShellHelperTest.cs └── ShellHelper.cs └── README.md /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3d2bc7a4498df4bafa2adb533ae6b8f0 3 | timeCreated: 1445577123 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 20ad999096cb2e946b8f1a9998184045 3 | folderAsset: yes 4 | timeCreated: 1445409190 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Editor/ShellHelper.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a9cabad4dc66e4d789a102130a67e9d4 3 | timeCreated: 1444972016 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Editor/ShellHelperTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: de7a0edcd55c749f7bc4df13a8100441 3 | timeCreated: 1445572203 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Attention 2 | 3 | check new version of unity shell which has better api design : 4 | 5 | https://github.com/wlgys8/UnityShell 6 | 7 | 8 | # UnityShellHelper 9 | 10 | This helper is designed for excuting shell command in unity editor. 11 | It will excute command with bash on osx & cmd.exe on win. 12 | 13 | Just drag the folder to your unity project and it will work. 14 | 15 | 16 | example: 17 | 18 | ShellHelper.ShellRequest req = ShellHelper.ProcessCommand("ls",""); 19 | req.onLog += delegate(int logType, string log) { 20 | Debug.Log(arg2); 21 | }; 22 | 23 | 24 | -------------------------------------------------------------------------------- /Editor/ShellHelperTest.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using UnityEditor; 4 | 5 | public class ShellHelperTest { 6 | 7 | 8 | [MenuItem("ShellHelperTest/ls")] 9 | public static void ls(){ 10 | #if UNITY_EDITOR_OSX 11 | ShellHelper.ShellRequest req = ShellHelper.ProcessCommand("ls",""); 12 | req.onLog += delegate(int arg1, string arg2) { 13 | Debug.Log(arg2); 14 | }; 15 | 16 | #endif 17 | } 18 | [MenuItem("ShellHelperTest/dir")] 19 | public static void dir(){ 20 | #if UNITY_EDITOR_WIN 21 | ShellHelper.ShellRequest req = ShellHelper.ProcessCommand("dir",""); 22 | req.onLog += delegate(int arg1, string arg2) { 23 | Debug.Log(arg2); 24 | }; 25 | 26 | #endif 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Editor/ShellHelper.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Diagnostics; 4 | using UnityEditor; 5 | using System.Collections.Generic; 6 | 7 | public class ShellHelper { 8 | 9 | public class ShellRequest{ 10 | public event System.Action onLog; 11 | public event System.Action onError; 12 | public event System.Action onDone; 13 | 14 | public void Log(int type,string log){ 15 | if(onLog != null){ 16 | onLog(type,log); 17 | } 18 | if(type == 1){ 19 | UnityEngine.Debug.LogError(log); 20 | } 21 | } 22 | 23 | public void NotifyDone(){ 24 | if(onDone != null){ 25 | onDone(); 26 | } 27 | } 28 | 29 | public void Error(){ 30 | if(onError != null){ 31 | onError(); 32 | } 33 | } 34 | } 35 | 36 | 37 | private static string shellApp{ 38 | get{ 39 | #if UNITY_EDITOR_WIN 40 | string app = "cmd.exe"; 41 | #elif UNITY_EDITOR_OSX 42 | string app = "bash"; 43 | #endif 44 | return app; 45 | } 46 | } 47 | 48 | 49 | private static List _queue = new List(); 50 | 51 | 52 | static ShellHelper(){ 53 | _queue = new List(); 54 | EditorApplication.update += OnUpdate; 55 | } 56 | private static void OnUpdate(){ 57 | for(int i = 0;i<_queue.Count;i++){ 58 | try{ 59 | var action = _queue[i]; 60 | if(action != null){ 61 | action(); 62 | } 63 | }catch(System.Exception e){ 64 | UnityEngine.Debug.LogException(e); 65 | } 66 | } 67 | _queue.Clear(); 68 | } 69 | 70 | 71 | 72 | public static ShellRequest ProcessCommand(string cmd,string workDirectory,List environmentVars = null){ 73 | ShellRequest req = new ShellRequest(); 74 | System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { 75 | Process p = null; 76 | try{ 77 | ProcessStartInfo start = new ProcessStartInfo(shellApp); 78 | 79 | #if UNITY_EDITOR_OSX 80 | string splitChar = ":"; 81 | start.Arguments = "-c"; 82 | #elif UNITY_EDITOR_WIN 83 | string splitChar = ";"; 84 | start.Arguments = "/c"; 85 | #endif 86 | 87 | if(environmentVars != null){ 88 | foreach(string var in environmentVars){ 89 | start.EnvironmentVariables["PATH"] += (splitChar + var); 90 | } 91 | } 92 | 93 | start.Arguments += (" \"" + cmd + " \""); 94 | start.CreateNoWindow = true; 95 | start.ErrorDialog = true; 96 | start.UseShellExecute = false; 97 | start.WorkingDirectory = workDirectory; 98 | 99 | if(start.UseShellExecute){ 100 | start.RedirectStandardOutput = false; 101 | start.RedirectStandardError = false; 102 | start.RedirectStandardInput = false; 103 | } else{ 104 | start.RedirectStandardOutput = true; 105 | start.RedirectStandardError = true; 106 | start.RedirectStandardInput = true; 107 | start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; 108 | start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; 109 | } 110 | 111 | p = Process.Start(start); 112 | p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { 113 | UnityEngine.Debug.LogError(e.Data); 114 | }; 115 | p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { 116 | UnityEngine.Debug.LogError(e.Data); 117 | }; 118 | p.Exited += delegate(object sender, System.EventArgs e) { 119 | UnityEngine.Debug.LogError(e.ToString()); 120 | }; 121 | 122 | bool hasError = false; 123 | do{ 124 | string line = p.StandardOutput.ReadLine(); 125 | if(line == null){ 126 | break; 127 | } 128 | line = line.Replace("\\","/"); 129 | 130 | _queue.Add(delegate() { 131 | req.Log(0,line); 132 | }); 133 | 134 | }while(true); 135 | 136 | while(true){ 137 | string error = p.StandardError.ReadLine(); 138 | if(string.IsNullOrEmpty(error)){ 139 | break; 140 | } 141 | hasError = true; 142 | _queue.Add(delegate() { 143 | req.Log(1,error); 144 | }); 145 | } 146 | p.Close(); 147 | if(hasError){ 148 | _queue.Add(delegate() { 149 | req.Error(); 150 | }); 151 | } 152 | else { 153 | _queue.Add(delegate() { 154 | req.NotifyDone(); 155 | }); 156 | } 157 | 158 | 159 | }catch(System.Exception e){ 160 | UnityEngine.Debug.LogException(e); 161 | if(p != null){ 162 | p.Close(); 163 | } 164 | } 165 | }); 166 | return req; 167 | } 168 | 169 | 170 | private List _enviroumentVars = new List(); 171 | 172 | public void AddEnvironmentVars(params string[] vars){ 173 | for(int i = 0;i