1、浏览本地文件
public static void DeleteGo()
{
string path1 = @"F:\BookCity_OutputBin\HuaShiDaUnity\assets\Res_BornEffects";
//System.Diagnostics.Process.Start("explorer.exe", path);
string path = EditorUtility.OpenFolderPanel(title, path1, string.Empty);
//string path = EditorUtility.OpenFilePanel(title, "*", "*");
DeleteEffFile(path);
Debug.Log("path: " + path);
}
这个作用是弹出一个浏览目录的窗口,然后手动选中一个文件夹的目录,最终将选择的文件夹的目录路径返回
2、浏览文件
public static void DeleteGo()
{
string path1 = @"F:\BookCity_OutputBin\HuaShiDaUnity\assets\Res_BornEffects";
//System.Diagnostics.Process.Start("explorer.exe", path);
//string path = EditorUtility.OpenFolderPanel(title, path1, string.Empty);
string path = EditorUtility.OpenFilePanel(title, "*", "*");
DeleteEffFile(path);
Debug.Log("path: " + path);
}
`
3、删除整个文件夹下指定的文件
public static void DeleteEffFile(string path)
{
if (!string.IsNullOrEmpty(path))
{
path = path.Replace('\\', '/');
//获取文件夹下的所有文件
//string[] files = Directory.GetFiles(path); //只能获取文件,不能获取文件夹
DirectoryInfo direction = new DirectoryInfo(path);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
Debug.Log("files: " + files.Length + ", path: " + path);
for (int i = 0; i < files.Length; ++i)
{
if (File.Exists(files[i].FullName))
{
//删除特效文件
if (files[i].FullName.EndsWith("_iOS.unity3d") ||
files[i].FullName.EndsWith("_WebGL.unity3d") ||
files[i].FullName.EndsWith("_Editor.unity3d"))
{
Debug.Log("delete file: " + files[i]);
File.Delete(files[i].FullName);
}
//删除AVPro控件
if (files[i].FullName.Contains("audio360")||
files[i].FullName.Contains("AVProVideo")||
files[i].FullName.Contains("Audio360") ||
files[i].FullName.Contains("libAVP") )
{
Debug.Log("delete file: " + files[i]);
File.Delete(files[i].FullName);
}
}
else
{
//如果是文件夹
DeleteEffFile(files[i].FullName);
}
}
}
}
4、文件打开参考(只有核心)
using System.Collections;
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
using UnityEngine;
using Application = UnityEngine.Application;
#if UNITY_EDITOR
using UnityEditor;
#endif
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDlg
{
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;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg : FileDlg
{
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SaveFileDlg : FileDlg
{
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenDirDlg
{
public IntPtr owner = IntPtr.Zero;
public IntPtr pidRoot = IntPtr.Zero;
public string displayName = null;
public string title = null;
public UInt32 flags = 0;
public IntPtr lpfn = IntPtr.Zero;
public IntPtr param = IntPtr.Zero;
public int image = 0;
}
public class FileDialogHelp
{
private static Form m_Form;
private static Form form
{
get
{
if (m_Form == null)
{
m_Form = new Form();
m_Form.WindowState = FormWindowState.Minimized;
m_Form.Show();
m_Form.TopMost = true;
m_Form.Hide();
m_Form.ShowIcon = false;
}
return m_Form;
}
}
private static IntPtr formIntPtr
{
get
{
return form.Handle;
}
}
//打开文件
#region OpenFileName
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
public static string OpenFileName(string title, string extString, string initialDirPref)
{
#if UNITY_EDITOR
string initialDir = PlayerPrefs.GetString(initialDirPref, Application.dataPath);
string path = EditorUtility.OpenFilePanel(title, initialDir, extString);
if (!string.IsNullOrEmpty(path))
{
path = path.Replace('\\', '/');
string dir_Pref = path.Substring(0, path.LastIndexOf('/'));
PlayerPrefs.SetString(initialDirPref, dir_Pref);
}
return path;
#else
string initialDir = PlayerPrefs.GetString(initialDirPref, Application.dataPath);
var dialog = new OpenFileDlg();
dialog.structSize = Marshal.SizeOf(dialog);
dialog.dlgOwner = formIntPtr;
dialog.filter = string.Format("{0} (*.{0})\0*.{0}\0", extString);
dialog.file = new string(new char[256]);
dialog.maxFile = dialog.file.Length;
dialog.fileTitle = new string(new char[64]);
dialog.maxFileTitle = dialog.fileTitle.Length;
dialog.initialDir = string.IsNullOrEmpty(initialDir) ? Application.dataPath : initialDir;
dialog.title = title;
dialog.defExt = string.Format(".{0}", extString);
dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (GetOpenFileName(dialog))
{
string path = dialog.file;
path = path.Replace('\\', '/');
string dir_Pref = path.Substring(0, path.LastIndexOf('/'));
PlayerPrefs.SetString(initialDirPref, dir_Pref);
return path;
}
else
{
return null;
}
#endif
}
#endregion
//保存文件
#region SaveFileName
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd);
public static string SaveFileName(string title, string extString, string initialDirPref)
{
#if UNITY_EDITOR
string initialDir = PlayerPrefs.GetString(initialDirPref, Application.dataPath);
string path = EditorUtility.SaveFilePanel(title, initialDir, string.Empty, extString);
if (!string.IsNullOrEmpty(path))
{
path = path.Replace('\\', '/');
string dir_Pref = path.Substring(0, path.LastIndexOf('/'));
PlayerPrefs.SetString(initialDirPref, dir_Pref);
}
return path;
#else
string initialDir = PlayerPrefs.GetString(initialDirPref, Application.dataPath);
var dialog = new SaveFileDlg();
dialog.structSize = Marshal.SizeOf(dialog);
dialog.dlgOwner = formIntPtr;
dialog.filter = string.Format("{0} (*.{0})\0*.{0}\0", extString);
dialog.file = new string(new char[256]);
dialog.maxFile = dialog.file.Length;
dialog.fileTitle = new string(new char[64]);
dialog.maxFileTitle = dialog.fileTitle.Length;
dialog.initialDir = string.IsNullOrEmpty(initialDir) ? Application.dataPath : initialDir;
dialog.title = title;
dialog.defExt = string.Format(".{0}", extString);
dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (GetSaveFileName(dialog))
{
string path = dialog.file;
path = path.Replace('\\', '/');
string dir_Pref = path.Substring(0, path.LastIndexOf('/'));
PlayerPrefs.SetString(initialDirPref, dir_Pref);
return path;
}
else
{
return null;
}
#endif
}
#endregion
//选择文件夹
#region OpenDirName
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern IntPtr SHBrowseForFolder([In, Out] OpenDirDlg ofn);
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
public static string OpenDirName(string title/*, string initialDir = null*/)
{
#if UNITY_EDITOR
string path = EditorUtility.OpenFolderPanel(title, Application.dataPath, string.Empty);
if (!string.IsNullOrEmpty(path))
{
path = path.Replace('\\', '/');
}
return path;
#else
OpenDirDlg dialog = new OpenDirDlg();
dialog.displayName = new string(new char[1024]);
dialog.owner = formIntPtr;
dialog.title = title;
var intptr = SHBrowseForFolder(dialog);
var chars = new char[1024];
for (int i = 0; i < chars.Length; i++)
{
chars[i] = '\0';
}
SHGetPathFromIDList(intptr, chars);
string path = new string(chars);
path = path.Substring(0, path.IndexOf('\0'));
if (!string.IsNullOrEmpty(path))
{
path = path.Replace('\\', '/');
}
return path;
#endif
}
#endregion
//弹出提示框
#region Message Box
public static void ShowMessage(string title, string message, MessageBoxIcon icon = MessageBoxIcon.Warning)
{
MessageBox.Show(form, message, title, MessageBoxButtons.OK, icon);
}
public static DialogResult ShowMessage_Complex(string title, string message, MessageBoxIcon icon = MessageBoxIcon.Warning)
{
return MessageBox.Show(form, message, title, MessageBoxButtons.YesNo, icon);
}
#endregion
}