Unity通过Process来调用dll或者exe

我们可以在unity中调用自己写的或者别的软件现成的dll或者exe来做一些事。
例如用7z来压缩或者解压文件。

直接上代码

 public class ExcuteCMD
    {
        //把命令行参数传入
        public static void DoCMD(string command)
        {
            using (Process p = new Process())
            {
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                //"/c" 代表执行接下来指定的命令,执行完后停止,会退出。例如:
                //了解更多cmd参数请进https://www.cnblogs.com/mq0036/p/5244892.html
                p.StartInfo.Arguments = "/c " +  command;
                //开始进程
                p.Start();
                //如果10秒后还没执行完成,那么结束这个进程
                p.WaitForExit(10000);
                //输出进程输出的内容
                Debug.Log(p.StandardOutput.ReadToEnd());
            }
        }
    }
 public class DeleteBuildConfig
      {
            [MenuItem("Tools/删除BuildConfig")]
            private static  void Excute()
            {
                //定义操作根目录
                  string rootPath = Application.dataPath + "/Plugins/Android/libs/";
                //定义输出目录
                  string outPath = rootPath + "test";
                //定义压缩文件路径
                  string filePath = rootPath + "test.jar";
                //定义7z提供的exe
                  string exePath = "\"D:/Softwares/Program Files/7-Zip/7z.exe\" ";
                //解压命令
                  string unZipStr = exePath + "x -o" + outPath + " " + filePath;
                //解压
                  ExcuteCMD.DoCMD(unZipStr);
                //删除原压缩文件,
                  File.Delete(filePath);
                //删除BuildConfig文件
                  string buildConfigPath = outPath + "/com/zjlbest/SDK_Demo/BuildConfig.class";
                  if (File.Exists(buildConfigPath))
                  {
                        File.Delete(buildConfigPath);
                      //压缩命令
                        string zipStr = exePath + "a " + filePath + " " + outPath + "/com/";
                      //压缩
                        ExcuteCMD.DoCMD(zipStr);
                  }
                //删除输出目录
                  Directory.Delete(outPath, true);
                //刷新游戏
                  AssetDatabase.Refresh();
            }
      }

以上就是一个简单的案例

你可能感兴趣的:(Unity通过Process来调用dll或者exe)