最近项目中,在做项目插件的时候涉及到了对资源管理器的一些基本操作,这里主要讲下在window下对资源管理器的选择,打开,保存的三个基本操作。
一、对指定文件的路径选择
/*
* 选择某个文件夹
*/
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SelectFile
{
public IntPtr hwndOwner = IntPtr.Zero;
public IntPtr pidlRoot = IntPtr.Zero;
public String pszDisplayName = null;
public String lpszTitle = null;
public UInt32 ulFlags = 0;
public IntPtr lpfn = IntPtr.Zero;
public IntPtr lParam = IntPtr.Zero;
public int iImage = 0;
}
public class SelectFileLog
{
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder([In, Out] SelectFile ofn);
[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
public static string GetSelectFileName()
{
SelectFile selectFileName = new SelectFile();
selectFileName.pszDisplayName = new string(new char[2000]);
selectFileName.lpszTitle = "选择文件夹";
IntPtr intPtr = SHBrowseForFolder(selectFileName);
char[] chArray = new char[2000];
for (int i = 0; i < chArray.Length; i++)
{
chArray[i] = '\0';
}
SHGetPathFromIDList(intPtr, chArray);
string fullPath = new string(chArray);
fullPath = fullPath.Substring(0, fullPath.IndexOf('\0'));
return fullPath;
}
}
我们这里采用editor下调用选择指定文件夹
[MenuItem("Tool/SelectFile")]
private static void SeleclFile()
{
string selectPath = SelectFileLog.GetSelectFileName();
Debug.LogError("选择的路径:" + selectPath);
}
下面调用结果:
二、关于打开文件的操作
我们这里以打开一张本地图片,并加载到场景中显示为例:
/*
* 打开指定文件或文件夹
*/
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
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;
}
public class OpenFileLog
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true,
CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool openFileName([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
public static void OpenFileName_()
{
OpenFileName openFile = new OpenFileName();
openFile.structSize = Marshal.SizeOf(openFile);
openFile.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
openFile.file = new string(new char[256]);
openFile.maxFile = openFile.file.Length;
openFile.fileTitle = new string(new char[64]);
openFile.maxFileTitle = openFile.fileTitle.Length;
//这里设置默认路径
string path = Application.streamingAssetsPath;
path = path.Replace('/', '\\');
openFile.initialDir = path;
openFile.title = "打开文件夹";
openFile.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (GetOpenFileName(openFile))
{
FileStream fileStream = new FileStream(openFile.file, FileMode.Open, FileAccess.Read);
fileStream.Seek(0, SeekOrigin.Begin);
//创建文件长度的缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, 0, (int)fileStream.Length);
fileStream.Close();
fileStream.Dispose();
fileStream = null;
//创建Texture
Texture2D texture2D = new Texture2D(100,100);
texture2D.LoadImage(bytes);
RawImage image = GameObject.Find("image").GetComponent<RawImage>();
if(image==null)
{
Debug.LogError("查找RawImages失败");
return;
}
image.texture = texture2D;
image.SetNativeSize();
}
}
}
加载结果:
三、选择将文件保存到本地指定路径
我们以保存一个txt文件为例进行操作:
public static void SaveFileName()
{
OpenFileName openFile = new OpenFileName();
openFile.structSize = Marshal.SizeOf(openFile);
openFile.filter = "txt文件(*.txt)\0*.txt";
openFile.file = new string(new char[256]);
openFile.maxFile = openFile.file.Length;
openFile.fileTitle = new string(new char[64]);
openFile.maxFileTitle = openFile.fileTitle.Length;
//这里设置默认路径
string path = Application.streamingAssetsPath;
path = path.Replace('/', '\\');
openFile.initialDir = path;
openFile.title = "保存本地文件";
openFile.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (GetSaveFileName(openFile))
{
FileInfo file = new FileInfo(openFile.file);
StreamWriter sw = file.CreateText();
sw.Write("test");
sw.Dispose();
sw.Close();
}
}
保存结束内容:
链接:https://pan.baidu.com/s/13sQ7e0Bh4lLX48LFTDeewQ
提取码:34md
想了解更多unity相关知识,可以关注下方公众号哦!!!