当前位置:网站首页>Unity中使用调用Shell的命令行
Unity中使用调用Shell的命令行
2022-06-11 22:05:00 【KindSuper_liu】
Unity中使用调用Shell的命令行
有的时候我们在做unity开发的时候需要去写一些工具编辑器之外的话通常会使用python,但有时也会使用shell命令行来完成一些操作,比如我们在写一自动化打包的时候 经常会使用一些linux指令来进行一些对文件或者文件夹的操作,这个时候使用shell就很实用。所以下面是一个shell的工具类来帮助我们可以在unity中更加轻松的使用 shell的一些指令。 c#代码如下:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using UnityEditor;
using System.Collections.Generic;
public class ShellHelper {
public class ShellRequest{
public event System.Action<int,string> onLog;
public event System.Action onError;
public event System.Action onDone;
public void Log(int type,string log){
if(onLog != null){
onLog(type,log);
}
if (type == 1) {
UnityEngine.Debug.LogError (log);
} else {
UnityEngine.Debug.Log (log);
}
}
public void NotifyDone(){
if(onDone != null){
onDone();
}
}
public void Error(){
if(onError != null){
onError();
}
}
}
private static string shellApp{
get{
#if UNITY_EDITOR_WIN
string app = "cmd.exe";
#elif UNITY_EDITOR_OSX
string app = "bash";
#endif
return app;
}
}
private static List<System.Action> _queue = new List<System.Action>();
static ShellHelper(){
_queue = new List<System.Action>();
EditorApplication.update += OnUpdate;
}
private static void OnUpdate(){
for(int i = 0;i<_queue.Count;i++){
try{
var action = _queue[i];
if(action != null){
action();
}
}catch(System.Exception e){
UnityEngine.Debug.LogException(e);
}
}
_queue.Clear();
}
public static void ProcessCommandSync(string cmd, string workDirectory, Dictionary<string, string> envVars = null) {
ProcessStartInfo start = new ProcessStartInfo(shellApp);
#if UNITY_EDITOR_OSX
start.Arguments = "-c";
#elif UNITY_EDITOR_WIN
start.Arguments = "/c";
#endif
if (envVars != null) {
foreach (var kv in envVars) {
if (start.EnvironmentVariables.ContainsKey(kv.Key)) {
start.EnvironmentVariables[kv.Key] = kv.Value;
} else {
start.EnvironmentVariables.Add(kv.Key, kv.Value);
}
}
}
start.Arguments += (" \"" + cmd + " \"");
start.CreateNoWindow = true;
start.ErrorDialog = true;
start.UseShellExecute = false;
start.WorkingDirectory = workDirectory;
start.RedirectStandardOutput = false;
start.RedirectStandardError = false;
start.RedirectStandardInput = false;
Process p = Process.Start(start);
p.WaitForExit();
UnityEngine.Debug.LogFormat("Finish running {0}", cmd);
}
public static ShellRequest ProcessCommand(string cmd, string workDirectory, Dictionary<string, string> envVars = null, bool isSync = false){
ShellRequest req = new ShellRequest();
System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) {
Process p = null;
try{
ProcessStartInfo start = new ProcessStartInfo(shellApp);
#if UNITY_EDITOR_OSX
start.Arguments = "-c";
#elif UNITY_EDITOR_WIN
start.Arguments = "/c";
#endif
if (envVars != null) {
foreach (var kv in envVars) {
if (start.EnvironmentVariables.ContainsKey(kv.Key)) {
start.EnvironmentVariables[kv.Key] = kv.Value;
} else {
start.EnvironmentVariables.Add(kv.Key, kv.Value);
}
}
}
start.Arguments += (" \"" + cmd + " \"");
start.CreateNoWindow = true;
start.ErrorDialog = true;
start.UseShellExecute = false;
start.WorkingDirectory = workDirectory;
if(start.UseShellExecute){
start.RedirectStandardOutput = false;
start.RedirectStandardError = false;
start.RedirectStandardInput = false;
} else{
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
}
p = Process.Start(start);
p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) {
UnityEngine.Debug.LogError(e.Data);
};
p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) {
UnityEngine.Debug.LogError(e.Data);
};
p.Exited += delegate(object sender, System.EventArgs e) {
UnityEngine.Debug.LogError(e.ToString());
};
bool hasError = false;
do{
string line = p.StandardOutput.ReadLine();
if(line == null){
break;
}
line = line.Replace("\\","/");
_queue.Add(delegate() {
req.Log(0,line);
});
}while(true);
while(true){
string error = p.StandardError.ReadLine();
if(string.IsNullOrEmpty(error)){
break;
}
hasError = true;
_queue.Add(delegate() {
req.Log(1,error);
});
}
p.Close();
if(hasError){
_queue.Add(delegate() {
req.Error();
});
}
else {
_queue.Add(delegate() {
req.NotifyDone();
});
}
}catch(System.Exception e){
UnityEngine.Debug.LogException(e);
if(p != null){
p.Close();
}
}
});
return req;
}
private Dictionary<string, string> _enviroumentVars = new Dictionary<string, string>();
public void AddEnvironmentVars(string key, string value){
if (key == null || value == null)
return;
if (string.IsNullOrEmpty(key.Trim()))
return;
if (_enviroumentVars.ContainsKey(key))
_enviroumentVars[key] = value;
_enviroumentVars.Add(key, value);
}
public ShellRequest ProcessCMD(string cmd,string workDir){
return ShellHelper.ProcessCommand(cmd,workDir,_enviroumentVars);
}
}
例子:使用MSBuild编译出Dll
[MenuItem("ILRuntime/拷贝Release Dll")]
public static void CopyReleaseDll()
{
string msbuildPath = "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin";
string exe = "MSBuild.exe";
string hotfixProject = Path.Combine(Environment.CurrentDirectory, "PlatformHotfix.csproj");
ShellHelper.ProcessCommandSync($"\"{
exe}\" \"{
hotfixProject}\" /t:Clean,Build /p:configuration=\"release\"", msbuildPath);
var folder = Path.GetDirectoryName(IL_HotFixFile_Dll1);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
if (File.Exists(Release_HotfixDll1))
{
File.Copy(Release_HotfixDll1, IL_HotFixFile_Dll1, true);
File.Copy(Release_HotfixPdb1, IL_HotFixFile_PDB1, true);
Debug.Log($"成功复制 Hotfix.dll, Hotfix.pdb到 目录 " + folder);
AssetDatabase.Refresh();
}
}
边栏推荐
猜你喜欢

Matplotlib和tkinter学习笔记(一)

Players must read starfish NFT advanced introduction

206. reverse linked list

The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace

crontab中定时执行shell脚本

How to view the installation date of the win system

The shortcomings of the "big model" and the strengths of the "knowledge map"

Classes and objects (1)

Classes and objects (3)

Top - k问题
随机推荐
R语言相关文章、文献整理合集(持续更新)
重温c语言一
All inherited features
The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
Take off efficiently! Can it be developed like this?
[Yu Yue education] basic engineering English of Zhejiang industrial and Commercial University (wuyiping) reference materials
Analysis of the implementation principle of an open source markdown to rich text editor
Glory earbud 3 Pro with three global first strong breakdowns flagship earphone Market
Is the securities account recommended by qiniu safe? Is it reliable
Tkinter学习笔记(三)
Use the securecrtportable script function to read data from network devices
6.项目上线
Static PVC with CEPH CSI
C language implements eight sorts (1)
Collection of articles and literatures related to R language (continuously updated)
Leetcode - day 2
3.2 测试类的命名规则
启牛推荐开通的证券账户安全吗?靠谱吗
[niuke.com] ky41 put apples
Sword finger offer array question type summary