1、先定义OpenFileName数据接收类
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
[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;
}
2、再定义系统函数调用类
public class LocalDialog
{
//链接指定系统函数 打开文件对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOFN([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
//链接指定系统函数 另存为对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
public static bool GetSFN([In,Out] OpenFileName ofn)
{
return GetSaveFileName(ofn);
}
}
3、调用文件类
public class DialogTest : MonoBehaviour
{
void OnGUI()
{
if (GUI.Button(new Rect(10,10,100,50),"Open"))
{
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "Excel文件(*.xlsx)\0*.xlsx";
openFileName.file = new string(new char[256]);//文件原始路径,格式(D:\3-图片-花.jpg)
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new string(new char[64]);//文件名字(带格式后缀),格式(3-图片-花.jpg)
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.initialDir = Application.streamingAssetsPath.Replace('/','\\');//默认路径
openFileName.title = "窗口标题";
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (LocalDialog.GetSaveFileName(openFileName))
{
Debug.Log(openFileName.file);
Debug.Log(openFileName.fileTitle);
}
}
}
}
4、文件格式
//示例
openFileName.filter = "图片文件(*jpg;*.png)\0*.jpg;*.png\0视频文件(*.mp4)\0*.mp4\0\0";
/*
()内为显示的提示文字;\0为通配符,以\0开头,\0结尾,文件格式写在里面
*/
格式:
6、 保存文件到指定文件夹
void Start()
{
string hhh = "D:/cat.jpg";
string savename = "newCat.jpg";
StartCoroutine(DownloadAndSave(hhh, savename, null));//保存文件。(路径,保存为名字)
}
public static IEnumerator DownloadAndSave(string url, string name, Action<bool, string> Finish = null)//路径,名字
{
url = Uri.EscapeUriString(url);
//url = "D:/Others/Dr/Project/U3D/1.3System/SqlV1/Assets/StreamingAssets/1-视频-cat";
Debug.Log(url);
string Loading = string.Empty;
bool b = false;
WWW www = new WWW(url);
if (www.error != null)
{
Debug.Log("error:" + www.error);
}
while (!www.isDone)
{
Debug.Log("没有完成");
Loading = (((int)(www.progress * 100)) % 100) + "%";
if (Finish != null)
{
Finish(b, Loading);
}
yield return 1;
}
if (www.isDone)
{
Debug.Log("完成了");
//Loading = "100%";
byte[] bytes = www.bytes;
b = SaveAssets(Application.streamingAssetsPath, name, bytes);
if (Finish != null)
{
Finish(b, Loading);
}
}
}
public static bool SaveAssets(string path, string name, byte[] bytes)//(保存路径,名字)
{
Stream sw;
FileInfo t = new FileInfo(path + "//" + name);//name需要为完整格式"DLAM.jpg"或者"DLAM.MP4"
if (!t.Exists)
{
try
{
sw = t.Create();
sw.Write(bytes, 0, bytes.Length);
sw.Close();
sw.Dispose();
return true;
}
catch
{
return false;
}
}
else
{
return true;
}
}