2023年11月29日发(作者:)

UnityC#如何执⾏cmd命令(s的使

⽤)

Unity中,我们可能需要⾃⼰写Editor⼯具。有时候我们可能还需要执⾏外部指令或者脚本(⽐如python脚本),这个时候,就需要⽤c#

s这个类了。

命名空间

using stics;

()的构造⽅法

名称说明

()启动(或重⽤)此 Process 组件的 StartInfo 属性指定的进程资源,并将其与该组件关联。

(ProcessStartInfo)

(String)通过指定⽂档或应⽤程序⽂件的名称来启动进程资源,并将资源与新的 Process 组件关联。

(String, String)

(String, String,

SecureString, String)Process 组件关联起来。

(String, String, String,

SecureString, String)

启动由包含进程启动信息(例如,要启动的进程的⽂件名)的参数指定的进程资源,并将该资

源与新的 Process 组件关联。

通过指定应⽤程序的名称和⼀组命令⾏参数来启动⼀个进程资源,并将该资源与新的 Process

组件相关联。

通过指定应⽤程序的名称、⽤户名、密码和域来启动⼀个进程资源,并将该资源与新的

通过指定应⽤程序的名称和⼀组命令⾏参数、⽤户名、密码和域来启动⼀个进程资源,并将该

资源与新的 Process 组件关联起来。

简单的接⼝封装

public static void RunCmd(string cmd, string args, string workdir=null)

{

string[] res = new string[2];

var p = CreateCmdProcess (cmd, args, workdir);

res [0] = End ();

res [1] = End ();

();

return res;

}

public static s CreateCmdProcess(string cmd, string args, string workdir=null)

{

var pStartInfo = new sStartInfo (cmd);

nts = args;

NoWindow = false;

llExecute = false;

ctStandardError = true;

ctStandardInput = true;

ctStandardOutput = true;

rdErrorEncoding = 8;

rdOutputEncoding= 8;

if(!string.IsNullOrEmpty(workdir))

gDirectory = workdir;

return (pStartInfo);

}

调⽤,假设我们想执⾏svn update的指令

var path = "D:Project";

RunCmd ("svn", string.Format("update "{0}"", path));

关于WorkingDirectory属性

WorkingDirectory属性指定可执⾏⽂件的位置。

如果WorkingDirectory是空字符串,则认为当前⽬录以包含可执⾏⽂件。

假设我们有⼀个py脚本D盘的py_pro⽬录中,即 D:py_

我们要执⾏这个脚本,需要这样⼦

var workdir= "D:py_pro";

RunCmd ("python", workdir + "", workdir);

// 打开记事本

("");

// 打开计算器

(" ");

// 打开注册表

(" ");

// 打开画图板

(" ");

// 打开写字板

(" ");

// 打开播放器

(" ");

// 打开任务管理器

(" ");

// 打开事件查看器

(" ");

// 打开系统信息

(" ");

// 打开Windows版本信息

(" ");

// 发邮件

("mailto: "+ address);

接⼝封装(详细版):

using System;

using UnityEditor;

using UnityEngine;

using c;

using ;

using ing;

using ;

///

/// 运⾏命令,不返回stderr版本

///

/// 命令

/// 命令的参数

/// StandardInput

/// ⼯作⽬录

/// 命令的stdout输出

public static string RunCmdNoErr(string cmd, string args, string[] input, string workingDri = "")

{

var p = CreateCmdProcess(cmd, args, workingDri);

if (input != null && > 0)

{

for (int i = 0; i < ; i++)

ine(input[i]);

}

var res = End();

();

return res;

}

///

/// 运⾏命令

///

/// 命令

/// 命令的参数

/// string[] res[0]命令的stdout输出, res[1]命令的stderr输出

public static string[] RunCmd(string cmd, string args, string workingDir = "")

{

string[] res = new string[2];

var p = CreateCmdProcess(cmd, args, workingDir);

res[0] = End();

res[1] = End();

#if !UNITY_IOS

res[2] = ng();

#endif

();

return res;

}

///

/// 打开⽂件夹

///

/// ⽂件夹的绝对路径

public static void OpenFolderInExplorer(string absPath)

{

if (rm == sEditor)

RunCmdNoErr("", absPath);

else if (rm == tor)

RunCmdNoErr("open", e("", "/"));

}

}

接⼝封装(详细版):

using System;

using UnityEditor;

using UnityEngine;

using c;

using ;

using ing;

using ;

class EdtUtil

ctStandardError = true;

ctStandardInput = true;

ctStandardOutput = true;

rdErrorEncoding = en;

rdOutputEncoding = en;

if (!string.IsNullOrEmpty(workingDir))

gDirectory = workingDir;

return (pStartInfo);

}

///

/// 运⾏命令,不返回stderr版本

///

/// 命令

/// 命令的参数

/// ⼯作⽬录

/// 命令的stdout输出

public static string RunCmdNoErr(string cmd, string args, string workingDri = "")

{

var p = CreateCmdProcess(cmd, args, workingDri);

var res = End();

();

return res;

}

///

/// 运⾏命令,不返回stderr版本

///

/// 命令

/// 命令的参数

/// StandardInput

/// ⼯作⽬录

/// 命令的stdout输出

public static string RunCmdNoErr(string cmd, string args, string[] input, string workingDri = "")

{

var p = CreateCmdProcess(cmd, args, workingDri);

if (input != null && > 0)

{

for (int i = 0; i < ; i++)

ine(input[i]);

}

var res = End();

();

return res;

}

///

/// 运⾏命令

///

/// 命令

/// 命令的参数

/// string[] res[0]命令的stdout输出, res[1]命令的stderr输出

public static string[] RunCmd(string cmd, string args, string workingDir = "")

{

string[] res = new string[2];

var p = CreateCmdProcess(cmd, args, workingDir);

res[0] = End();

res[1] = End();

#if !UNITY_IOS

res[2] = ng();

#endif

();

return res;

}

///

/// 打开⽂件夹

///

/// ⽂件夹的绝对路径

public static void OpenFolderInExplorer(string absPath)

{

if (rm == sEditor)

RunCmdNoErr("", absPath);

else if (rm == tor)

RunCmdNoErr("open", e("", "/"));

}

}

使⽤

上⾯的RunCmd接⼝,returnstring[],第0个是脚本的输出,第1个是错误输出

⽐如通过上⾯的RunCmd接⼝运⾏下⾯的python脚本

#python脚本: , 放在Assets/python⽬录中

print("Hello, I am python!")

print("This is a test")

try:

print(1/0)

except BaseException,e:

print("Error: " + str(e))

//c# 通过RunCmd接⼝执⾏python脚本

var workdir = th + "/python/";

var results = ("python", workdir + "", workdir);

("python output: " + results[0]);

("python error: " + results[1]);

输出的结果是

python output: Hello, I am python!

This is a test

python error: Error: integer division or module by zero