189 lines
7.8 KiB
C#
189 lines
7.8 KiB
C#
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.IO;
|
||
using System;
|
||
using System.Runtime.InteropServices;
|
||
|
||
namespace GameEditor.Util
|
||
{
|
||
|
||
/*这是C#引用非托管的C/C++的DLL的一种定义定义结构体的方式,主要是为了内存中排序,LayoutKind有两个属性Sequential和Explicit
|
||
Sequential表示顺序存储,结构体内数据在内存中都是顺序存放的Explicit表示精确布局,需要用FieldOffset()设置每个成员的位置这都是
|
||
为了使用非托管的指针准备的,CharSet=CharSet.Ansi表示编码方式
|
||
*/
|
||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||
public struct OpenFileName
|
||
{
|
||
public int structSize; //结构的内存大小
|
||
public IntPtr dlgOwner; //设置对话框的句柄
|
||
public IntPtr instance; //根据flags标志的设置,确定instance是谁的句柄,不设置则忽略
|
||
public string filter; //调取文件的过滤方式
|
||
public string customFilter; //一个静态缓冲区 用来保存用户选择的筛选器模式
|
||
public int maxCustFilter; //缓冲区的大小
|
||
public int filterIndex; //指向的缓冲区包含定义过滤器的字符串对
|
||
public string file; //存储调取文件路径
|
||
public int maxFile; //存储调取文件路径的最大长度 至少256
|
||
public string fileTitle; //调取的文件名带拓展名
|
||
public int maxFileTitle; //调取文件名最大长度
|
||
public string initialDir; //最初目录
|
||
public string title; //打开窗口的名字
|
||
public int flags; //初始化对话框的一组位标志 参数类型和作用查阅官方API
|
||
public short fileOffset; //文件名前的长度
|
||
public short fileExtension; //拓展名前的长度
|
||
public string defExt; //默认的拓展名
|
||
public IntPtr custData; //传递给lpfnHook成员标识的钩子子程的应用程序定义的数据
|
||
public IntPtr hook; //指向钩子的指针。除非Flags成员包含OFN_ENABLEHOOK标志,否则该成员将被忽略。
|
||
public string templateName; //模块中由hInstance成员标识的对话框模板资源的名称
|
||
public IntPtr reservedPtr;
|
||
public int reservedInt;
|
||
public int flagsEx; //可用于初始化对话框的一组位标志
|
||
}
|
||
|
||
public class WindowDll
|
||
{
|
||
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
|
||
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
|
||
}
|
||
|
||
public static class ProcessUtil
|
||
{
|
||
|
||
//
|
||
public static void ProcessCommand(string dir, string com, bool isShowWindow = false)
|
||
{
|
||
ProcessCommand(dir, new string[] { com }, isShowWindow);
|
||
}
|
||
public static void ProcessCommand(string dir, string[] coms, bool isShowWindow = false)
|
||
{
|
||
#if UNITY_IOS
|
||
string shellPath = Path.Combine(dir, "coms.sh");
|
||
if (!File.Exists(shellPath))
|
||
{
|
||
File.Create(shellPath).Close();
|
||
}
|
||
File.WriteAllLines(shellPath, coms);
|
||
|
||
using(System.Diagnostics.Process p = new System.Diagnostics.Process())
|
||
{
|
||
|
||
//设置要启动的应用程序
|
||
p.StartInfo.FileName = "/bin/zsh";
|
||
//是否使用操作系统shell启动
|
||
p.StartInfo.UseShellExecute = false;
|
||
//接受来自调用程序的输入信息
|
||
p.StartInfo.RedirectStandardInput = false;
|
||
//输出信息
|
||
p.StartInfo.RedirectStandardOutput = false;
|
||
//输出错误
|
||
p.StartInfo.RedirectStandardError = false;
|
||
//不显示程序窗口
|
||
p.StartInfo.CreateNoWindow = !isShowWindow;
|
||
//设置文件夹
|
||
p.StartInfo.WorkingDirectory = dir;
|
||
p.StartInfo.Arguments = shellPath;
|
||
//启动程序
|
||
p.Start();
|
||
//等待程序执行完退出进程
|
||
p.WaitForExit();
|
||
//关闭
|
||
p.Close();
|
||
//删除命令行文件
|
||
File.Delete(shellPath);
|
||
|
||
}
|
||
#else
|
||
System.Diagnostics.Process p = new System.Diagnostics.Process();
|
||
//设置要启动的应用程序
|
||
p.StartInfo.FileName = "cmd.exe";
|
||
//是否使用操作系统shell启动
|
||
p.StartInfo.UseShellExecute = false;
|
||
//接受来自调用程序的输入信息
|
||
p.StartInfo.RedirectStandardInput = false;
|
||
//输出信息
|
||
p.StartInfo.RedirectStandardOutput = false;
|
||
//输出错误
|
||
p.StartInfo.RedirectStandardError = false;
|
||
//不显示程序窗口
|
||
p.StartInfo.CreateNoWindow = !isShowWindow;
|
||
// 设置文件夹
|
||
p.StartInfo.WorkingDirectory = dir;
|
||
// 执行命令
|
||
for (int i = 0; i < coms.Length; i++)
|
||
{
|
||
p.StartInfo.Arguments = "/k " + coms[i] + "&exit";
|
||
//启动程序
|
||
p.Start();
|
||
//等待程序执行完退出进程
|
||
p.WaitForExit();
|
||
|
||
}
|
||
//p.StartInfo.Arguments = "/k exit";
|
||
////启动程序
|
||
//p.Start();
|
||
//等待程序执行完退出进程
|
||
//p.WaitForExit();
|
||
p.Close();
|
||
#endif
|
||
}
|
||
|
||
|
||
|
||
|
||
// 打开文本
|
||
public static void OpenText(string tPath)
|
||
{
|
||
if (!File.Exists(tPath))
|
||
{
|
||
EditorUtility.DisplayDialog("提示", "文件不存在", "确定");
|
||
return;
|
||
}
|
||
#if PLATFORM_IOS
|
||
ProcessCommand(Application.dataPath, "open " + tPath); //用记事本
|
||
|
||
#else
|
||
System.Diagnostics.Process.Start("notepad.exe", tPath); //用记事本
|
||
|
||
#endif
|
||
}
|
||
//返回 数组第一个是文件夹路径 ,以后的是文件名字
|
||
public static string[] OpenFileWin(string path,string ext = "*.*")
|
||
{
|
||
//初始化
|
||
OpenFileName ofn = new OpenFileName();
|
||
ofn.structSize = Marshal.SizeOf(ofn);
|
||
ofn.filter = "All Files\0*.*\0\0";
|
||
ofn.file = new string(new char[1024]);
|
||
ofn.maxFile = ofn.file.Length;
|
||
ofn.fileTitle = new string(new char[64]);
|
||
ofn.maxFileTitle = ofn.fileTitle.Length;
|
||
path = path.Replace('/', '\\');
|
||
ofn.initialDir = path; //默认路径
|
||
ofn.title = "Open Project";
|
||
ofn.defExt = ext;//显示文件的类型
|
||
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
|
||
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
|
||
string[] strs = null;
|
||
//判断是否打开文件
|
||
if (WindowDll.GetOpenFileName(ofn))
|
||
{
|
||
//多选文件
|
||
string[] Splitstr = { "\0" };
|
||
string[] strs1 = ofn.file.Split(Splitstr, StringSplitOptions.RemoveEmptyEntries);
|
||
if (strs1.Length == 1)
|
||
{
|
||
strs = new string[] { Path.GetDirectoryName(strs1[0]), Path.GetFileName(strs1[0]) };
|
||
}
|
||
else
|
||
{
|
||
strs = strs1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogFormat("路径为空");
|
||
}
|
||
|
||
return strs;
|
||
}
|
||
}
|
||
} |