Unity C#執行bat腳本的操作

我們先封裝一下接口,如下,把EdtUtil.cs放置在Assets/Editor目錄中

// EdtUtil.cs 
using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Text; 
class EdtUtil
{
    public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "")
    {
        var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
        pStartInfo.Arguments = args;
        pStartInfo.CreateNoWindow = false;
        pStartInfo.UseShellExecute = true;
        pStartInfo.RedirectStandardError = false;
        pStartInfo.RedirectStandardInput = false;
        pStartInfo.RedirectStandardOutput = false;
        if (!string.IsNullOrEmpty(workingDir))
            pStartInfo.WorkingDirectory = workingDir;
        return System.Diagnostics.Process.Start(pStartInfo);
    }
    
    public static void RunBat(string batfile, string args, string workingDir = "")
    {
        var p = CreateShellExProcess(batfile, args, workingDir);
        p.Close();
    }
    
    public static string FormatPath(string path)
    {
        path = path.Replace("/", "\\");
        if (Application.platform == RuntimePlatform.OSXEditor)
            path = path.Replace("\\", "/");
    }
}

現在,我們在工程Assets外層有一個batFiles目錄,裡面有一個gen_client_cfg.bat腳本

我們想通過Unity菜單執行這個腳本,例

using UnityEngine;
using UnityEditor;
 
class Test
{
    private static void RunMyBat(string batFile,string workingDir)
    {
        var path = EdtUtil.FormatPath(workingDir);
        if (!System.IO.File.Exists(path))
        {
            GameLogger.LogError("bat文件不存在:" + path);
        }
        else
        {
            EdtUtil.RunBat(batFile, "", path);
        }
    }
    
    [MenuItem("Tools/執行gen_client_cfg.bat")]
    private static void Run()
    {
        // 執行bat腳本
        RunBat("gen_client_cfg.bat", Application.dataPath + "/../batFiles/");
    }
}

點擊菜單 【Tools】-【執行gen_client_cfg.bat】即可在Unity中直接執行bat腳本瞭

補充:unity運行bat文件並隱藏cmd窗口

懶散幾年瞭,今天重拾學習計劃。

Unity中調用bat文件的方法和因此cmd窗口的設置:

需要添加庫

using System.Diagnostics;

方法代碼:

public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "")
    {
        var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
        pStartInfo.Arguments = args;
        pStartInfo.CreateNoWindow = false;
        pStartInfo.UseShellExecute = true;
       // pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pStartInfo.RedirectStandardError = false;
        pStartInfo.RedirectStandardInput = false;
        pStartInfo.RedirectStandardOutput = false;
        if (!string.IsNullOrEmpty(workingDir))
            pStartInfo.WorkingDirectory = workingDir;
        return System.Diagnostics.Process.Start(pStartInfo);
    }
    public  void RunBat(string batfile, string args, string workingDir = "")
    {
        var p = CreateShellExProcess(batfile, args, workingDir);
        p.Close();
    }

上面代碼註釋掉的那行就是隱藏窗口的方法。需要註意的是:

如果proc.StartInfo.UseShellExecute為false,使用:

proc.StartInfo.CreateNoWindow = true;

如果proc.StartInfo.UseShellExecute為true,通過以下方式為進程進行設置:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

關閉開啟的程序代碼:

static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "")
 {
        var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
        pStartInfo.Arguments = args;
        pStartInfo.CreateNoWindow = false;
        pStartInfo.UseShellExecute = true;
        pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pStartInfo.RedirectStandardError = false;
        pStartInfo.RedirectStandardInput = false;
        pStartInfo.RedirectStandardOutput = false;
        if (!string.IsNullOrEmpty(workingDir))
            pStartInfo.WorkingDirectory = workingDir;
        return System.Diagnostics.Process.Start(pStartInfo);
    }
    public void RunBat(string batfile, string args, string workingDir = "")
    {
        var p = CreateShellExProcess(batfile, args, workingDir);
        p.Close();
    }

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。