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#自动类型转换与强制类型转换的讲解

    C#自动类型转换与强制类型转换的讲解

    今天小编就为大家分享一篇关于C#自动类型转换与强制类型转换的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • c#多种加解密示例(md5加密解密)

    c#多种加解密示例(md5加密解密)

    这篇文章主要介绍了c#多种加解密示例,包括了MD5加密,SHA1加密,DES加解密,需要的朋友可以参考下
    2014-03-03
  • 浅析WPF中Binding的数据校验和类型转换

    浅析WPF中Binding的数据校验和类型转换

    在WPF开发中,Binding实现了数据在Source和Target之间的传递和流通,那在WPF开发中,如何实现数据的校验和类型转换呢,下面就跟随小编一起学习一下吧
    2024-03-03
  • C#使用Aspose.Cells创建和读取Excel文件

    C#使用Aspose.Cells创建和读取Excel文件

    这篇文章主要为大家详细介绍了C#使用Aspose.Cells创建和读取Excel文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • C#结合JS修改解决KindEditor弹出层问题

    C#结合JS修改解决KindEditor弹出层问题

    KindEditor 是一款出色的富文本HTML在线编辑器,这里我们讲述在使用中遇到的一个问题,在部署到某些 WEB 应用项目中,点击类似弹出层功能时,只显示了遮罩层,而内容层则定位无法正确显示,所以本文给大家介绍了C#结合JS 修改解决 KindEditor 弹出层问题
    2024-06-06
  • C#实现Excel导入sqlite的方法

    C#实现Excel导入sqlite的方法

    这篇文章主要介绍了C#实现Excel导入sqlite的方法,是C#程序设计中非常重要的一个实用技巧,需要的朋友可以参考下
    2014-09-09
  • C#递归读取XML菜单数据的方法

    C#递归读取XML菜单数据的方法

    这篇文章主要介绍了C#递归读取XML菜单数据的方法,涉及递归的操作技巧与C#窗体的用法,对于进行C#项目开发具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • c# 遍历 Dictionary的四种方式

    c# 遍历 Dictionary的四种方式

    这篇文章主要介绍了c# 遍历 Dictionary的四种方式,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-09-09
  • C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)

    C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)

    在工作中经常遇到C#数组、ArrayList、List、Dictionary存取数据,但是该选择哪种类型进行存储数据呢?很迷茫,今天小编抽空给大家整理下这方面的内容,需要的朋友参考下吧
    2017-02-02
  • 基于WPF实现弹幕效果的示例代码

    基于WPF实现弹幕效果的示例代码

    这篇文章主要为大家详细介绍了如何利用WPF实现弹幕效果,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2022-09-09

最新评论