此文仅作分享,之前的内容有个BUG,也是最近才发现,当点击选择窗口的时候发现点击UnitypLayer窗口的时候,WinDialog窗口被置于Unity的下面去了。然后就无法进行文件选择,之前没考虑到这个问题,最近有体验者说有些人点开了不知道是干啥的,直接就忽略了,去点Unity的软件。结果就出现BUG,交互不了,于是乎就有了以下的优化。
优化点:WinDialog窗口被顶置。使用者只能选择,要么取消;
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class DialogConfig
{
#region Config Field
public int structSize = 0;//设置窗口大小
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;//文件类型
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;//指定路劲
public String title = null;//窗口名称
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
#endregion
}
public class WindowDialog
{
#region Win32API WRAP
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName([In, Out] DialogConfig dialog); //这个方法名称必须为GetOpenFileName
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
static extern bool GetSaveFileName([In, Out] DialogConfig dialog); //这个方法名称必须为GetSaveFileName
#endregion
/// <summary>
/// 打开文件选择窗口
/// </summary>
public static string OpenDialogChoiseFile()
{
DialogConfig ofn = new DialogConfig();
ofn.structSize = Marshal.SizeOf(ofn);
//这里设置文件类型。可以指定视频,图片等其他文件。*.*默认为所有文件
ofn.filter = "文件(*.mp4; *.mp4)\0 *.mp4; *.mp4";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
//默认路径
ofn.initialDir = Application.streamingAssetsPath;
ofn.title = "请选择文件:";
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
ofn.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶。
if (!GetOpenFileName(ofn))
{
return null;
}
return ofn.file;
}
/// <summary>
/// 保存文件选择窗口
/// </summary>
/// <param name="title">指定窗口名称</param>
/// <param name="extensions">预设文件存储位置及文件名</param>
/// <returns>文件路径</returns>
public static string OpenDialogSaveFile(string path,string[]filters)
{
var extension = Path.GetExtension(path);
DialogConfig ofn = new DialogConfig();
ofn.structSize = Marshal.SizeOf(ofn);
// ofn.filter = Filter("All Files", "*.*", extension, "*" + extension);
ofn.filterIndex = 0;
var chars = new char[256];
var it = Path.GetFileName(path).GetEnumerator();
for (int i = 0; i < chars.Length && it.MoveNext(); ++i)
{
chars[i] = it.Current;
}
ofn.file = new string(chars);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = Path.GetDirectoryName(path);
ofn.title = "请选择文件保持路径:";
ofn.flags = 0x00000002 | 0x00000004; // OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
ofn.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶。
if (!GetSaveFileName(ofn))
{
return null;
}
return ofn.file;
}
}
调用方法:
string path = WindowDialog.OpenDialogChoiseFile();
print(path);
效果: