unity调用windows窗口选择文件并保存

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;
        }
    }

7、参考文献

Unity中调用Windows窗口选择文件 - 苍鼠 - 博客园 (cnblogs.com)

openFileName.filter过滤器的语法规则_ytz201201的博客-CSDN博客

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值