Unity操作文件对话框

一、运行时操作文件对话框

调用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 path
		pth.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 path
		pth.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 Files
		FileFilterType 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 path
		pth.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自动添加
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45686837/article/details/123523872
今日推荐