Unity操作文件对话框
unity文件对话框操作
- 一、运行时操作文件对话框
- 调用Win32操作对话框
- 1.打开对话框;保存对话框;调用windows窗口对文件进行筛选功能,文件类型自定义
- 2.实现步骤
- 二、编辑时操作文件对话框
一、运行时操作文件对话框
调用Win32操作对话框
(1)OpenFileDialog控件的基本属性
- InitialDirectory:对话框的初始目录
- Filter:获取或设置当前文件名筛选器字符串,
例如:文本文件 "txt (.txt)";
所有文件 (.)||.* - FilterIndex在对话框中选择的文件筛选器的索引,如果选第一项就设为1
- RestoreDirectory 控制对话框在关闭之前是否恢复当前目录
- FileName:第一个在对话框中显示的文件或最后一个选取的文件
- Title 将显示在对话框标题栏中的字符
- AddExtension是否自动添加默认扩展名
- CheckPathExists 在对话框返回之前,检查指定路径是否存在
- DefaultExt 默认扩展名
- DereferenceLinks 在从对话框返回前是否取消引用快捷方式
- ShowHelp 启用"帮助"按钮
- ValiDateNames控制对话框检查文件名中是否不含有无效的字符或序列
(2)OpenFileDialog控件有以下常用事件
- FileOk 当用户点击"打开"或"保存"按钮时要处理的事件
- HelpRequest 当用户点击"帮助"按钮时要处理的事件
1.打开对话框;保存对话框;调用windows窗口对文件进行筛选功能,文件类型自定义
(1)打开对话框
(2)保存对话框
(3)调用windows窗口对文件进行筛选功能,文件类型自定义
主要是:设置filter,并且结合pth.filterIndex去选择使用生成的类型列表中的第几个(注意是从1开始的)
//\0字符串分隔符(多个根据\0分割的字符串形成下拉列表);
//pth.filter = "JSON file(*.json)\0*.json\0FBX file(*.fbx)\0*.fbx\0"; //* \0*.fbx";
//;是&的作用 1:.json;.fbx;.gltf 2:.json 3:.fbx 4:.gltf 5:PNG 6:All Files
pth.filter = "模型文件(*.json|*.fbx|*.gltf)\0*.json;*.fbx;*.gltf\0JSON file(*.json)\0*.json\0FBX file(*.fbx)\0*.fbx\0GLTF file(*.gltf)\0*.gltf\0PNG file(*.png)\0*.png\0All Files\0*.*\0\0";
2.实现步骤
(1)创建窗口对话框类:FileDialog
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System;[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDialog
{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 OpenFileDlg : FileDialog
{}
public class OpenFileDialog
{[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SaveFileDlg : FileDialog
{}
public class SaveFileDialog
{[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd);
}
(2)创建DialogManager类,调用刚才FileDialog,
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;public class DialogManager:MonoBehaviour
{Action<string> action;/// <summary>/// 打开项目弹框/// </summary>/// <returns></returns>public void OpenWindowDialog(){string filepath = "";OpenFile pth = new OpenFile();pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);// pth.filter = "JSON file(*.json)";//是什么文件类型就修改此处pth.filter = "All Files\0*.*\0\0";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; // default pathpth.title = "打开项目"; //标题pth.defExt = "json";//注意 一下项目不一定要全选 但是0x00000008项不要缺少//0x00080000 是否使用新版文件选择窗口,0x00000200 是否可以多选文件pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (OpenFileWindow.GetOpenFileName(pth)){filepath = pth.file;//选择的文件路径;action(filepath);}}/// <summary>/// 创建保存场景/另存场景面板/// </summary>/// <param name="titleName">面板主题名</param>/// <param name="filePath">保存路径</param>public void SaveOrSaveAsWindowDialog(){SaveFile pth = new SaveFile();pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);pth.filter = "All Files\0 *.*\0\0";//是什么文件类型就修改此处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; // default pathpth.title = "保存项目";pth.defExt = "json";pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (SaveFileWindow.GetSaveFileName(pth)){string filepath = pth.file;//选择的文件路径;action(filepath);}}/// <summary>/// 获取SceneName/// </summary>/// <param name="name">路径名字</param>/// <returns></returns>private string GetSceneName(string name){string[] names = name.Split('\\');string myname = names[names.Length - 1].Split('.')[0];return myname;}/// <summary>/// 文件过滤器类型/// </summary>public enum FileFilterType{/// <summary>/// 模型文件(.json/.gltf/.fbx) 1/// </summary>Model_File,/// <summary>/// json文件(.json) 2/// </summary>JSON_File,/// <summary>/// FBX文件(.fbx) 3/// </summary>FBX_File,/// <summary>/// GLTF文件(.gltf) 4/// </summary>GLTF_File,/// <summary>/// PNG文件(.png) 5/// </summary>PNG_File,/// <summary>/// ALLFile 6/// </summary>AllFile,}/// <summary>/// 调用windows窗口对文件进行筛选功能,文件类型自定义,打开项目弹框/// </summary>/// <returns></returns>public void OpenWindowFilterDialog(){string filepath = "";OpenFile pth = new OpenFile();pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);//\0字符串分隔符(多个根据\0分割的字符串形成下拉列表);//pth.filter = "JSON file(*.json)\0*.json\0FBX file(*.fbx)\0*.fbx\0"; //* \0*.fbx";//;是&的作用 1:.json;.fbx;.gltf 2:.json 3:.fbx 4:.gltf 5:PNG 6:All FilesFileFilterType fileFilterType=new FileFilterType();pth.filter = "模型文件(*.json|*.fbx|*.gltf)\0*.json;*.fbx;*.gltf\0JSON file(*.json)\0*.json\0FBX file(*.fbx)\0*.fbx\0GLTF file(*.gltf)\0*.gltf\0PNG file(*.png)\0*.png\0All Files\0*.*\0\0";pth.filterIndex = ((int)fileFilterType + 1);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; // default pathpth.title = "打开挑选项目";pth.defExt = "txt"; //默认扩展名//注意 一下项目不一定要全选 但是0x00000008项不要缺少//0x00080000 是否使用新版文件选择窗口,0x00000200 是否可以多选文件pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;if (OpenFileWindow.GetOpenFileName(pth)){filepath = pth.file;//选择的文件路径;action(filepath);}}}
(4)创建“打开对话框”,“保存对话框”,“筛选对话框”三个Button,分别挂载FileDialogControllor的对应方法
(4)运行
二、编辑时操作文件对话框
使用EditorUtility.OpenFilePanel或者EditorUtility.OpenFolderPanel方法
(1)创建EditedDialogController类
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;public class EditedDialogController : MonoBehaviour
{[MenuItem("Custom/OpenFile")]public static void OpenFile(){string file = EditorUtility.OpenFilePanel("Open File Dialog", "D:\\", "exe");Debug.Log(file);}[MenuItem("Custom/OpenFolder")]public static void OpenFolder(){string file = EditorUtility.OpenFolderPanel("Open Folder Dialog", "D:\\", "unity");Debug.Log(file);}}
(2)保存之后unity自动添加
Unity操作文件对话框相关推荐
- VC++一文带你搞懂如何操作文件对话框(附源码)
VC++常用功能开发汇总(专栏文章列表,欢迎订阅,持续更新...)https://blog.csdn.net/chenlycly/article/details/124272585C++软件异常排查从 ...
- python打开-Python中的打开文件对话框(转)
1.最早学习Tkinter的时候,在<Tkinter编程代码实例>中看到的"打开文件对话框"需要用到FileDialog模块,代码非常简单: from Tkinter ...
- MFC实现打开、保存文件对话框和浏览文件夹对话框,把代码直接拷贝到要响应的按钮函数下面就行了
MFC实现打开.保存文件对话框和浏览文件夹对话框,把代码直接拷贝到要响应的按钮函数下面就行了 一.打开.保存对话框 文件对话框属于通用对话框范畴(另外还有颜色,查找,查找替换,字体,打印等对话框). ...
- java swing对话框_Java开发笔记(一百三十五)Swing的文件对话框
除了常规的提示对话框,还有一种对话框也很常见,它叫做文件对话框.文件对话框又分为两小类:打开文件的对话框.保存文件的对话框,但在Swing中它们都用类型JFileChooser来表达.下面是JFile ...
- 使用VBA操作文件(1):使用Excel对话框
转贴自 http://www.excelperfect.com/2009/08/05/handlefileswithvba/ 使用VBA操作文件(1):使用Excel对话框 本专题主要讲述使用VBA ...
- unity 修改文件后缀_unity文件操作-添加。修改。删除
支持原创:http://www.manew.com/thread-23491-1-1.html. 相信大家在开发过程中,难免会保存一些文件在客户端进行本地化操作. 如:配置文件,状态文件,Assetb ...
- 第八章节 文件操作一 (文件对话框)
1.文件对话框 openFileDialog对话框和saveFileDialog对话框的属性和方法一模一样 openFIleDialog和saveFileDialog的使用方式一模一样,就是功能上面有 ...
- VC 文件操作(文件查找,打开/保存,读/写,复制/删除/重命名)
右击项目->属性->字符集:使用多字节字符集. 这样可以使用char到CString的转化. char sRead[20] = ""; CString strtest ...
- c# 操作文件_小练习(音乐播放器)
form排版如图一所示,详细步骤内容请见代码块:这个小练习主要目的是为了更加熟练的掌握Path类和熟练掌握操作文件的方法: using System; using System.Collections ...
- VS2010/MFC编程入门之十七(对话框:文件对话框)
上一讲鸡啄米介绍的是消息对话框,本节讲解文件对话框.文件对话框也是很常用的一类对话框. 文件对话框的分类 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见 ...
最新文章
- 5G NPN 行业专网 — 部署模式
- javascript中的变量
- php内存映射,如何用ZwMapViewOfSection将Driver分配的内存映射到App空间?
- ui设计卡片阴影_UI设计形状和对象基础知识:阴影和模糊
- cpanel java_Cpanel是什么
- 按字节提取整形数值(按位与运算符“”、右移位运算符“”)
- XCode: 如何添加自定义代码片段
- 12.看板方法---度量和管理报告
- 软件测试论坛_进阶测试攻略——价值驱动的软件测试
- 芝麻小客服怎么进后台?
- 【深度学习】基于MindSpore和pytorch的Softmax回归及前馈神经网络
- 聚类分析 | MATLAB实现k-Means(k均值聚类)分析
- ioc的概念和实现原理
- 海航控股公布重整计划 海航“航”向何方?
- linux 中.a和.so的区别
- CDISC SDTM IE domain学习笔记
- 迪文DWIN串口屏的使用经验分享
- 论文要点总结:Gradient-Based Learning Applied to Document Recognition(一)
- 让文章排版看起来更舒服的方法
- 被一个假AT45DB161整到残废