untiy 打开windows资源浏览器 选择一个本地文件

有时候我们会需要让用户自己导入一个视频或图片到untiy,此时需要使用windows自己的资源浏览器

直接调用这个脚本的OpenResManager方法即可,返回值为选中的文件的绝对路径

public class LoadResourceByFileDlg : IUtility
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();  //用于将一个窗口置顶
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern bool GetOpenFileName([In, Out] DialogConfig dialog);  //这个方法名称必须为GetOpenFileName   用于打开读取文件窗口
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    private static extern bool GetSaveFileName([In, Out] DialogConfig dialog);  //这个方法名称必须为GetSaveFileName  用于打开保存文件窗口
    //这个类的作用是为打开文件浏览器的外部方法GetOpenFileName提供一个配置参数,类名可以自定义,但是属性必须是固定的
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] //StructLayout允许我们自己定义属性在内存中顺序  Sequential为按顺序排列
    public class DialogConfig
    {
        #region Config Field
        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;
        #endregion
    }
    /// 
    /// 打开文件浏览器选取文件
    /// 
    public string OpenResManager()
    {
        DialogConfig ofn = new DialogConfig();
        ofn.structSize = Marshal.SizeOf(ofn);
        //这里设置文件类型。可以指定视频,图片等其他文件。*.*默认为所有文件
        //ofn.filter = "文件(*.mp4; *.mp4)\0 *.mp4; *.mp4";
        ofn.filter = "文件(*.*)|*.*";
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        //默认路径
        ofn.initialDir = GameApp.Interface.GetModel<SystemModel>().PersonImportExcelPath;
        ofn.title = "请选择文件:";
        //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR 
        ofn.dlgOwner = GetForegroundWindow(); //这一步将文件选择窗口置顶。
        if (!GetOpenFileName(ofn))
        {
            return null;
        }
        return ofn.file;
    }   
}

另附将通过上述脚本获取到视频名后,拷贝到本地方法

    private void OnImportVideo(string fullName)//导入视频
    {
        Debug.Log("文件名" + fullName);
        if (string.IsNullOrEmpty(fullName))
        {
            return;
        }
        string extension = Path.GetExtension(fullName);
        if (!(extension == ".mp4" || extension == ".avi" || extension == ".wmv"))
        {
            Debug.log("仅支持 .mp4 .avi .wmv 视频格式"));
            return;
        }
        string fileNme = Path.GetFileName(fullName);//获取文件名
        //复制文件
        File.Copy(fullName, sceneTrainingModel.videoFolerPath + "/" + fileNme);
    }

你可能感兴趣的:(unity,windows,unity)