这篇文章主要介绍了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();
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
织梦狗教程
本文标题为:Unity C#执行bat脚本的操作


基础教程推荐
猜你喜欢
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- Unity shader实现高斯模糊效果 2023-01-16
- C# 解析XML和反序列化的示例 2023-04-14
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#中 Json 序列化去掉null值的方法 2022-11-18