Unity通過Process來調(diào)用dll或者exe

我們可以在unity中調(diào)用自己寫的或者別的軟件現(xiàn)成的dll或者exe來做一些事。
例如用7z來壓縮或者解壓文件。

直接上代碼

 public class ExcuteCMD
    {
        //把命令行參數(shù)傳入
        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" 代表執(zhí)行接下來指定的命令,執(zhí)行完后停止,會退出。例如:
                //了解更多cmd參數(shù)請進(jìn)https://www.cnblogs.com/mq0036/p/5244892.html
                p.StartInfo.Arguments = "/c " +  command;
                //開始進(jìn)程
                p.Start();
                //如果10秒后還沒執(zhí)行完成,那么結(jié)束這個(gè)進(jìn)程
                p.WaitForExit(10000);
                //輸出進(jìn)程輸出的內(nèi)容
                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();
            }
      }

以上就是一個(gè)簡單的案例

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容