您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 2浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity在Windows平台下使用打开文件对话框、保存文件对话框、浏览文件夹

程序员正茂 发布时间:2020-09-23 10:30:16 ,浏览量:2

1、定义FileDlog类
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDlog
{
    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 OpenDialogDir
{
    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;
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg : FileDlog
{
}
2、使用打开文件对话框
OpenFileDlg ofd = new OpenFileDlg();
        ofd.structSize = Marshal.SizeOf(ofd);
        ofd.filter = "(*.shp;*.tif;*.las;*.lfp)\0*.shp;*.tif;*.las;*.lfp\0所有文件\0*.*\0\0";
        ofd.file = new string(new char[256]);
        ofd.maxFile = ofd.file.Length;
        ofd.fileTitle = new string(new char[64]);
        ofd.maxFileTitle = ofd.fileTitle.Length;
        ofd.initialDir = Application.dataPath; //默认路径
        ofd.title = "打开文件";
        ofd.defExt = "csv";
        ofd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetOpenFileName(ofd))
        {
            string filepath = ofd.file; //选择的文件路径;  
            Debug.Log(filepath);
        }
3、使用保存文件对话框
OpenFileDialog sfd = new OpenFileDialog();
        sfd.structSize = Marshal.SizeOf(sfd);
        sfd.filter = "CSV (逗号分隔)(*.csv)\0*.csv";
        sfd.file = new string(new char[256]);
        sfd.maxFile = sfd.file.Length;
        sfd.fileTitle = new string(new char[64]);
        sfd.maxFileTitle = sfd.fileTitle.Length;
        sfd.initialDir = Application.dataPath; //默认路径
        sfd.title = "保存文件";
        sfd.defExt = "csv";
        sfd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetSaveFileName(sfd))
        {
            string filepath = sfd.file; //选择的文件路径;             
        }
3、浏览文件夹
      OpenDialogDir ofn2 = new OpenDialogDir();
        ofn2.pszDisplayName = new string(new char[2000]);// 存放目录路径缓冲区    
        ofn2.lpszTitle = "Open Project";
        IntPtr pidlPtr = OpenFileDialog.SHBrowseForFolder(ofn2);

        char[] charArray = new char[2000];
        for (int i = 0; i < 2000; i++)
            charArray[i] = '\0';

        OpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
        string fullDirPath = new String(charArray);
        fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
        Debug.Log(fullDirPath);

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0350s