欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity C#執(zhí)行bat腳本的操作

 更新時間:2021年04月09日 10:40:26   作者:林新發(fā)  
這篇文章主要介紹了Unity C#執(zhí)行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("\\", "/");
    }
}

現(xiàn)在,我們在工程Assets外層有一個batFiles目錄,里面有一個gen_client_cfg.bat腳本

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

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/執(zhí)行g(shù)en_client_cfg.bat")]
    private static void Run()
    {
        // 執(zhí)行bat腳本
        RunBat("gen_client_cfg.bat", Application.dataPath + "/../batFiles/");
    }
}

點擊菜單 【Tools】-【執(zhí)行g(shù)en_client_cfg.bat】即可在Unity中直接執(zhí)行bat腳本了

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

懶散幾年了,今天重拾學(xué)習(xí)計劃。

Unity中調(diào)用bat文件的方法和因此cmd窗口的設(shè)置:

需要添加庫

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,通過以下方式為進程進行設(shè)置:

proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

關(guān)閉開啟的程序代碼:

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();
    }

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • C# 設(shè)計模式系列教程-模板方法模式

    C# 設(shè)計模式系列教程-模板方法模式

    模板方法模式通過把不變的行為搬移到超類,去除了子類中的重復(fù)代碼,子類實現(xiàn)算法的某些細節(jié),有助于算法的擴展。
    2016-06-06
  • C#Url操作類封裝、仿Node.Js中的Url模塊實例

    C#Url操作類封裝、仿Node.Js中的Url模塊實例

    這篇文章主要介紹了C#Url操作類封裝、仿Node.Js中的Url模塊,實例分析了C#Url操作類封裝的技巧,非常具有實用價值,需要的朋友可以參考下。
    2016-10-10
  • C#計算程序執(zhí)行過程花費時間的方法

    C#計算程序執(zhí)行過程花費時間的方法

    這篇文章主要介紹了C#計算程序執(zhí)行過程花費時間的方法,涉及C#簡單的時間運算技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#使用三層架構(gòu)開發(fā)Winform的詳細案例

    C#使用三層架構(gòu)開發(fā)Winform的詳細案例

    這篇文章介紹了C#使用三層架構(gòu)開發(fā)Winform的詳細案例,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 詳解C#中HashTable的用法

    詳解C#中HashTable的用法

    在.NET Framework中,Hashtable是System.Collections命名空間提供的一個容器,用于處理和表現(xiàn)類似keyvalue的鍵值對,其中key通??捎脕砜焖俨檎遥瑫rkey是區(qū)分大小寫;value用于存儲對應(yīng)于key的值
    2016-02-02
  • C#創(chuàng)建一個小型Web Server(Socket實現(xiàn))

    C#創(chuàng)建一個小型Web Server(Socket實現(xiàn))

    這篇文章主要介紹了關(guān)于C#利用Socket實現(xiàn)創(chuàng)建一個小型Web Server的相關(guān)資料,文中通過示例代碼介紹的很詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • NancyFx框架檢測任務(wù)管理器詳解

    NancyFx框架檢測任務(wù)管理器詳解

    這篇文章主要為大家詳細介紹了NancyFx框架檢測任務(wù)管理器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別

    C#中析構(gòu)函數(shù)、Dispose、Close方法的區(qū)別

    本文詳細對比了C#中析構(gòu)函數(shù)、Dispose和Close方法的區(qū)別,三者都是釋放資源,本文介紹了他們各自的使用方法和使用場景,希望對大家有所幫助。
    2016-04-04
  • 在C#中使用MSMQ的方法

    在C#中使用MSMQ的方法

    這篇文章主要介紹了在C#中使用MSMQ的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C#使用selenium實現(xiàn)操作瀏覽器并且截圖

    C#使用selenium實現(xiàn)操作瀏覽器并且截圖

    這篇文章主要為大家詳細介紹了C#如何使用selenium組件實現(xiàn)操作瀏覽器并且截圖,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考一下
    2024-01-01

最新評論