using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System;
public class Form_Window : MonoBehaviour, Form
{
public const string filter_Image = "图片文件(*.jpg;*.png)\0*.jpg;*.png";
public const string filter_All = "所有文件(*.*)\0*.*";
public const string filter_Excel = "excel(*.xls;*.xlsx)\0*.xls;*.xlsx\0xlsx(*.xlsx)\0*.xlsx\0xls(*.xls)\0*.xls";
private void Start()
{
string folderPath = UnityEngine.Application.streamingAssetsPath;
}
public void SelectFile(string folderPath = null, string filter = null, System.Action<FileData> callBack = null)
{
switch (0)
{
case 0:
case 1:
SelectFile_Dialog(folderPath, filter, callBack);
break;
case 2:
SelectFile_Dll(folderPath, filter, callBack);
break;
}
}
private void SelectFile_Dll(string folderPath = null, string filter = null, System.Action<FileData> callBack = null)
{
System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Filter = filter.Replace("\0","|");
dialog.InitialDirectory = folderPath;
if (dialog.ShowDialog() == DialogResult.OK)
{
FileData fileData = new FileData();
fileData.FileDatas = dialog.FileName;
fileData.ToString();
callBack?.Invoke(fileData);
}
}
private void SelectFile_Dialog(string folderPath = null, string filter = null,
System.Action<FileData> callBack = null)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog = dialog.GetOpenFileDialog(folderPath, filter);
if (WindowDll.GetOpenFileName(dialog))
{
FileData fileData = new FileData();
fileData.FileDatas = dialog.file;
fileData.ToString();
callBack?.Invoke(fileData);
}
}
public void SelectSaveFile(string folderPath = null, string filter = null, Action<string, bool> callBack = null)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog = dialog.GetOpenFileDialog(folderPath, filter, true);
if (WindowDll.GetSaveFileName(dialog))
{
string filePath = dialog.file;
callBack?.Invoke(filePath, System.IO.File.Exists(filePath));
}
}
public void SelectFolder(string folderPath = null, System.Action<string> callBack = null)
{
switch (0)
{
case 0:
SelectFolder_Ookii(folderPath, callBack);
break;
case 1:
SelectFolder_Dialog(folderPath, callBack);
break;
case 2:
SelectFolder_Dll(folderPath, callBack);
break;
}
}
private void SelectFolder_Ookii(string folderPath = null, System.Action<string> callBack = null)
{
Ookii.Dialogs.VistaFolderBrowserDialog dialog = new Ookii.Dialogs.VistaFolderBrowserDialog();
dialog.ShowNewFolderButton = true;
dialog.SelectedPath = folderPath;
if (dialog.ShowDialog() == DialogResult.OK)
{
callBack?.Invoke(dialog.SelectedPath);
Debug.Log(dialog.SelectedPath);
}
}
private void SelectFolder_Dll(string folderPath = null, System.Action<string> callBack = null)
{
System.Windows.Forms.FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = true;
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
dialog.SelectedPath = folderPath;
if (dialog.ShowDialog() == DialogResult.OK)
{
callBack?.Invoke(dialog.SelectedPath);
Debug.Log(dialog.SelectedPath);
}
}
private void SelectFolder_Dialog(string folderPath = null, System.Action<string> callBack = null)
{
OpenFolderDialog ofn2 = new OpenFolderDialog();
ofn2.pszDisplayName = new string(new char[2048]);
ofn2.ulFlags = 0x00000040;
IntPtr pidlPtr = WindowDll.SHBrowseForFolder(ofn2);
int count = 2048;
char[] charArray = new char[count];
for (int i = 0; i < count; i++)
{
charArray[i] = '\0';
}
WindowDll.SHGetPathFromIDList(pidlPtr, charArray);
string res = new string(charArray);
res = res.Substring(0, res.IndexOf('\0'));
Debug.Log(res);
callBack?.Invoke(res);
}
public void OpenFolder(string path)
{
System.Diagnostics.Process.Start("explorer.exe", path);
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFileDialog
{
public int structSize;
public IntPtr dlgOwner;
public IntPtr instance;
public string filter;
public string customFilter;
public int maxCustFilter;
public int filterIndex;
public string file;
public int maxFile;
public string fileTitle;
public int maxFileTitle;
public string initialDir;
public string title;
public int flags;
public short fileOffset;
public short fileExtension;
public string defExt;
public IntPtr custData;
public IntPtr hook;
public string templateName;
public IntPtr reservedPtr;
public int reservedInt;
public int flagsEx;
public OpenFileDialog GetOpenFileDialog(string folderPath = null, string filter = null, bool isSave = false)
{
OpenFileDialog dialog = this;
dialog.structSize = Marshal.SizeOf(dialog);
dialog.filter = filter;
dialog.initialDir = folderPath;
dialog.file = new string(new char[1024]);
dialog.maxFile = dialog.file.Length;
dialog.fileTitle = new string(new char[64]);
dialog.maxFileTitle = dialog.fileTitle.Length;
if (!isSave)
{
dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
}
else
{
dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
}
return dialog;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFolderDialog
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public String pszDisplayName;
public String lpszTitle;
public UInt32 ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}
public class WindowDll
{
[DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileDialog dialog);
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenFileDialog ofn);
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder([In, Out] OpenFolderDialog ofn);
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Serialization;
public interface Form
{
public void SelectFile(string folderPath = null, string filter = null, System.Action<FileData> callBack = null);
public void SelectSaveFile(string folderPath = null, string filter = null, Action<string, bool> callBack = null);
public void SelectFolder(string folderPath = null, System.Action<string> callBack = null);
public void OpenFolder(string path);
}
public struct FileData
{
public string FolderPath;
public string[] FilePath;
public string[] FileName;
private string fileData;
public string FileDatas
{
set
{
fileData = value;
FolderPath = null;
FilePath = null;
FileName = null;
string[] Splitstr = { "\0" };
string[] strs = value.Split(Splitstr, StringSplitOptions.RemoveEmptyEntries);
int len = strs.Length;
if (len == 1)
{
string path = strs[0].Replace("\\", "/");
FilePath = new[] { path };
int index = path.LastIndexOf('/');
FolderPath = path.Substring(0, index);
index++;
string fileName = path.Substring(index, path.Length - index);
FileName = new[] { fileName };
}
else if (len > 1)
{
len -= 1;
FolderPath = strs[0].Replace("\\", "/");
FileName = new string[len];
FilePath = new string[len];
for (int i = 0; i < len; i++)
{
FileName[i] = strs[i + 1];
FilePath[i] = FolderPath + "/" + strs[i + 1];
}
}
}
}
public string ToString()
{
StringBuilder selectFileBuilder = GetStringBuilder(FilePath);
StringBuilder selectFileNameBuilder = GetStringBuilder(FileName);
string str =
$"Folder: {FolderPath}\r\nFile: {selectFileBuilder.ToString()}\r\nFileName: {selectFileNameBuilder.ToString()}";
Debug.Log(str);
return str;
}
private StringBuilder GetStringBuilder(string[] arr)
{
StringBuilder builder = new StringBuilder();
if (arr != null)
{
int len = arr.Length;
for (int i = 0; i < len; i++)
{
builder.AppendLine(arr[i]);
}
}
return builder;
}
}
public class SystemFormMgr : Singleton<SystemFormMgr>
{
private Form form_Window;
public Form SystemForm
{
get
{
if (form_Window == null)
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
form_Window = new Form_Window();
#elif UNITY_ANDROID || UNITY_IPHONE
form_Window = new Form_Android();
#elif UNITY_IOS
form_Window = new Form_IOS();
#endif
}
return form_Window;
}
}
}