服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity C#执行bat脚本的操作

Unity C#执行bat脚本的操作

2022-11-10 14:13林新发 C#

这篇文章主要介绍了Unity C#执行bat脚本的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我们先封装一下接口,如下,把EdtUtil.cs放置在Assets/Editor目录中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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菜单执行这个脚本,例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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窗口的设置:

需要添加库

?
1
using System.Diagnostics;

方法代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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,使用:

?
1
proc.StartInfo.CreateNoWindow = true;

如果proc.StartInfo.UseShellExecute为true,通过以下方式为进程进行设置:

?
1
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

关闭开启的程序代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
    }

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

原文链接:https://linxinfa.blog.csdn.net/article/details/78690488

延伸 · 阅读

精彩推荐
  • C#C#无损高质量压缩图片代码

    C#无损高质量压缩图片代码

    这篇文章主要为大家详细介绍了C#无损高质量压缩图片代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    边界流浪者9422022-01-05
  • C#C#深度优先遍历实现全排列

    C#深度优先遍历实现全排列

    这篇文章主要介绍了C#深度优先遍历实现全排列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随...

    Leaderxin5272022-08-10
  • C#C#如何给PDF文件添加水印

    C#如何给PDF文件添加水印

    这篇文章主要为大家详细介绍了C#如何给PDF文件添加水印的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    Yesi7112021-12-07
  • C#C#条码生成类分享

    C#条码生成类分享

    这篇文章主要分享了C#条码生成类的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    孤者自清4462022-01-11
  • C#C# 使用multipart form-data方式post数据到服务器

    C# 使用multipart form-data方式post数据到服务器

    这篇文章主要介绍了C# 使用multipart form-data方式post数据到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    三五月儿3672022-10-07
  • C#英语单词state与status的区别

    英语单词state与status的区别

    state倾向于condition,是一种延续性的状态。status常用于描述一个过程中的某阶段(phase),类似于C语言中枚举型变量某一个固定的值,这个值属于一个已知...

    likebeta8552021-12-09
  • C#详解WPF中的对象资源

    详解WPF中的对象资源

    这篇文章主要介绍了WPF中对象资源的相关资料,帮助大家更好的理解和学习使用WPF,感兴趣的朋友可以了解下...

    杜文龙4972022-11-10
  • C#C#枚举类型和结构体详解

    C#枚举类型和结构体详解

    这篇文章主要为大家详细介绍了C#枚举类型和结构体,,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ☆☆☆☆☆11592021-12-06