最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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窗口

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

Unity中調(diào)用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();
    }

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

相關文章

  • c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例

    c#中Linq to Sql 增刪除的實例,需要的朋友可以參考一下
    2013-05-05
  • 淺談C#各種數(shù)組直接的數(shù)據(jù)復制/轉(zhuǎn)換

    淺談C#各種數(shù)組直接的數(shù)據(jù)復制/轉(zhuǎn)換

    下面小編就為大家?guī)硪黄獪\談C#各種數(shù)組直接的數(shù)據(jù)復制/轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • 如何使用正則表達式判斷郵箱(以C#為例)

    如何使用正則表達式判斷郵箱(以C#為例)

    在C#中可以使用Regex正則表達式類來校驗前臺提交過來的郵箱字段信息是否符合要求,Regex類是C#中有關正則表達式處理的相關類,功能強大,下面這篇文章主要給大家介紹了關于如何使用正則表達式判斷郵箱的相關資料,需要的朋友可以參考下
    2022-03-03
  • C# Newtonsoft.Json用法詳解

    C# Newtonsoft.Json用法詳解

    本文主要介紹了C# Newtonsoft.Json用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法

    C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法

    這篇文章主要介紹了C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法,涉及C#針對sql server數(shù)據(jù)庫的簡單查詢技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型

    帶著問題讀CLR via C#(筆記一)CLR的執(zhí)行模型

    CLR (Common Language Runtime) 是一個可以由多種編程語言使用的“運行時”。
    2013-04-04
  • C#實現(xiàn)右鍵快捷菜單(上下文菜單)的兩種方式

    C#實現(xiàn)右鍵快捷菜單(上下文菜單)的兩種方式

    在C#中,ContextMenuStrip是一種用于創(chuàng)建右鍵菜單的控件,它提供了一種方便的方式來為特定的控件或窗體添加自定義的上下文菜單選項,有兩種實現(xiàn)方式,文中介紹的非常詳細,需要的朋友可以參考下
    2024-03-03
  • 如何使用C# 捕獲進程輸出

    如何使用C# 捕獲進程輸出

    這篇文章主要介紹了如何使用C# 捕獲進程輸出,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-08-08
  • C# 中TaskScheduler的使用小結(jié)

    C# 中TaskScheduler的使用小結(jié)

    本文主要介紹了C# 中TaskScheduler的使用小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-12-12
  • Unity中的PostProcessBuild實用案例深入解析

    Unity中的PostProcessBuild實用案例深入解析

    這篇文章主要為大家介紹了Unity中的PostProcessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05

最新評論

台北市| 永吉县| 云安县| 游戏| 当阳市| 安乡县| 长丰县| 阿拉善盟| 阿拉善盟| 新蔡县| 尤溪县| 南雄市| 静乐县| 卓资县| 中牟县| 承德县| 太仓市| 商河县| 枝江市| 廉江市| 永州市| 舞阳县| 曲麻莱县| 瓦房店市| 平乐县| 五常市| 寻甸| 永登县| 大荔县| 彭州市| 建湖县| 富阳市| 景洪市| 贺州市| 舟曲县| 丰台区| 永修县| 凤庆县| 西青区| 全南县| 永川市|