Unity打开文件OpenFileDialog

39 篇文章 1 订阅

一、打开文件对话框(OpenFileDialog)

1、 OpenFileDialog控件的基本属性

  • InitialDirectory:对话框的初始目录 
  • Filter: 获取或设置当前文件名筛选器字符串,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*" 
  • FilterIndex 在对话框中选择的文件筛选器的索引,如果选第一项就设为1 
  • RestoreDirectory 控制对话框在关闭之前是否恢复当前目录 
  • FileName:第一个在对话框中显示的文件或最后一个选取的文件 
  • Title 将显示在对话框标题栏中的字符 
  • AddExtension 是否自动添加默认扩展名 
  • CheckPathExists 在对话框返回之前,检查指定路径是否存在 
  • DefaultExt 默认扩展名 
  • DereferenceLinks 在从对话框返回前是否取消引用快捷方式 
  • ShowHelp 启用"帮助"按钮 
  • ValiDateNames 控制对话框检查文件名中是否不含有无效的字符或序列

2、 OpenFileDialog控件有以下常用事件

FileOk 当用户点击"打开"或"保存"按钮时要处理的事件 
HelpRequest 当用户点击"帮助"按钮时要处理的事件

代码演示:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class FileDialogHelper : MonoBehaviour
{
    private static FileDialogHelper _instance;
    private string ScreenShotPath = Application.dataPath.Replace('/', '\\') + "\\GameScreenShot";

    public static FileDialogHelper Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new FileDialogHelper();
            }
            return _instance;
        }
    }

    /// <summary>
    /// 打开项目
    /// </summary>
    public void OpenProject(Action luafunc, string defaultPath)
    {
        if (!System.IO.Directory.Exists(ScreenShotPath))
        {
            System.IO.Directory.CreateDirectory(ScreenShotPath);
        }
        defaultPath = ScreenShotPath;
        Action<Action, string> _OpenProject = OpenOpenFileDlg;
        _OpenProject.BeginInvoke(luafunc, defaultPath, null, null);
    }

    private void OpenOpenFileDlg(Action luafunc, string defaultPath)
    {
        OpenFileDlg pth = new OpenFileDlg();
        pth.structSize = Marshal.SizeOf(pth);
        //pth.filter = "All files (*.*)|*.*";
        pth.filter = "图片文件(*.jpg,*.png)\0*.jpg;*.png";
        pth.file = new string(new char[256]);
        pth.maxFile = pth.file.Length;
        pth.fileTitle = new string(new char[64]);
        pth.maxFileTitle = pth.fileTitle.Length;
        pth.initialDir = defaultPath; //默认路径//@"%userprofile%\Pictures";//@"D:\";//Application.dataPath.Replace("/", "\\") + "\\Resources"; //默认路径
        pth.title = "选择文件";
        pth.defExt = "png";//显示文件的类型
        pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        pth.dlgOwner = OpenFileDialog.GetForegroundWindow();
        if (OpenFileDialog.GetOpenFileName(pth))
        {
            string filepath = pth.file; //选择的文件路径; 
            string filename = pth.fileTitle;
            if (luafunc != null)
            {
                luafunc();
            }
        }
        else
        {
            if (luafunc != null)
            {
                luafunc();
            }
        }
    }

    /// <summary>
    /// 保存文件项目
    /// </summary>
    public void SaveProject()
    {
        try
        {
            OpenFileDlg pth = new OpenFileDlg();
            pth.structSize = Marshal.SizeOf(pth);
            pth.filter = "*.jpg|*.png";
            pth.file = new string(new char[256]);
            pth.maxFile = pth.file.Length;
            pth.fileTitle = new string(new char[64]);
            pth.maxFileTitle = pth.fileTitle.Length;
            pth.initialDir = Application.dataPath; //默认路径
            pth.title = "保存项目";
            //pth.defExt = "dat";
            pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
            pth.dlgOwner = OpenFileDialog.GetForegroundWindow();
            if (OpenFileDialog.GetSaveFileName(pth))
            {
                string filepath = pth.file; //选择的文件路径;  
                Debug.Log(filepath);
            }
        }
        catch (Exception)
        {
        }
    }



    public void ScreenShot()
    {
        if (!System.IO.Directory.Exists(ScreenShotPath))
        {
            System.IO.Directory.CreateDirectory(ScreenShotPath);
        }
        string filename = "/" + DateTime.Now.ToString("yyyyMdHms") + ".png";
        UnityEngine.ScreenCapture.CaptureScreenshot(ScreenShotPath + filename);
    }

}

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Auto)]
public class OpenFileDlg
{
    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 OpenFileDialog
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] OpenFileDlg ofd);
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();
}

3、 获取对话框的文件名

openfiledialog.FileName //获取或设置一个包含在文件对话框中选定的文件名字符串

openfiledialog.SafeFileName  //获取选定对话框中的文件名和扩展名

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值