Unity C#执行bat脚本的操作

 更新时间:2021年04月09日 10:40:26   作者:林新发  
这篇文章主要介绍了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();
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • c#中Linq to Sql 增删除的实例

    c#中Linq to Sql 增删除的实例

    c#中Linq to Sql 增删除的实例,需要的朋友可以参考一下
    2013-05-05
  • 浅谈C#各种数组直接的数据复制/转换

    浅谈C#各种数组直接的数据复制/转换

    下面小编就为大家带来一篇浅谈C#各种数组直接的数据复制/转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    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数据库名称的方法

    C#获取所有SQL Server数据库名称的方法

    这篇文章主要介绍了C#获取所有SQL Server数据库名称的方法,涉及C#针对sql server数据库的简单查询技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 带着问题读CLR via C#(笔记一)CLR的执行模型

    带着问题读CLR via C#(笔记一)CLR的执行模型

    CLR (Common Language Runtime) 是一个可以由多种编程语言使用的“运行时”。
    2013-04-04
  • C#实现右键快捷菜单(上下文菜单)的两种方式

    C#实现右键快捷菜单(上下文菜单)的两种方式

    在C#中,ContextMenuStrip是一种用于创建右键菜单的控件,它提供了一种方便的方式来为特定的控件或窗体添加自定义的上下文菜单选项,有两种实现方式,文中介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • 如何使用C# 捕获进程输出

    如何使用C# 捕获进程输出

    这篇文章主要介绍了如何使用C# 捕获进程输出,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-08-08
  • C# 中TaskScheduler的使用小结

    C# 中TaskScheduler的使用小结

    本文主要介绍了C# 中TaskScheduler的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-12-12
  • Unity中的PostProcessBuild实用案例深入解析

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

    这篇文章主要为大家介绍了Unity中的PostProcessBuild实用案例深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05

最新评论