unity3d:打开windows文件浏览框,实现打开,另存为

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


public class WindowsDialog : MonoBehaviour
{

    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern bool GetOpenFileName([In, Out] FileName ofn);
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern bool GetSaveFileName([In, Out] FileName ofd);
    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern IntPtr SHBrowseForFolder([In, Out] DirName ofn);

    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
    public static string Load(params string[] ext)
    {
        FileName i = new FileName(ext);
        i.title = "打开";
        GetOpenFileName(i);
        return i.file;
    }
    public static string Save(string ext)
    {
        FileName i = new FileName(ext);
        i.title = "保存";
        GetSaveFileName(i);
        return i.file;
    }
    public static string GetDir()
    {
        DirName d = new DirName();
        IntPtr i = SHBrowseForFolder(d);
        char[] c = new char[256];
        SHGetPathFromIDList(i, c);
        return new string(c);
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private class DirName
    {
        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 DirName()
        {
            pszDisplayName = new string(new char[256]);
            ulFlags = 0x00000040 | 0x00000010; //BIF_NEWDIALOGSTYLE | BIF_EDITBOX;
            lpszTitle = "打开目录";
        }
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private class FileName
    {
        public int structSize = 0;
        private IntPtr dlgOwner = IntPtr.Zero;
        private IntPtr instance = IntPtr.Zero;
        private string filter = null;
        private string customFilter = null;
        private int maxCustFilter = 0;
        private int filterIndex = 0;
        public string file { get; set; }
        private int maxFile = 0;
        public string fileTitle { get; set; }
        private int maxFileTitle = 0;
        public string initialDir { get; set; }
        public string title { get; set; }
        private int flags = 0;
        private short fileOffset = 0;
        private short fileExtension = 0;
        private string defExt = null;
        private IntPtr custData = IntPtr.Zero;
        private IntPtr hook = IntPtr.Zero;
        private string templateName = null;
        private IntPtr reservedPtr = IntPtr.Zero;
        private int reservedInt = 0;
        private int flagsEx = 0;
        public FileName(params string[] ext)
        {
            structSize = Marshal.SizeOf(this);
            defExt = ext[0];
            string n = null;
            string e = null;
            foreach (string _e in ext)
            {
                if (_e == "*")
                {
                    n += "All Files";
                    e += "*.*;";
                }
                else
                {
                    string _n = "." + _e + ";";
                    n += _n;
                    e += "*" + _n;
                }
            }
            n = n.Substring(0, n.Length - 1);
            filter = n + "\0" + e + "\0";
            file = new string(new char[256]);
            maxFile = file.Length;
            fileTitle = new string(new char[64]);
            maxFileTitle = fileTitle.Length;
            //flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
            flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
            initialDir = Application.dataPath;
        }

    }
}

猜你喜欢

转载自blog.csdn.net/luoyikun/article/details/81011431