Unity3d实现手动选择动态加载PPT文件并展示
前言
上一篇文章我讲了Unity3d实现加载PPT文件并展示
没看的自行查看:
https://blog.csdn.net/qq_33789001/article/details/115083422
之前实现的是加载固定的ppt文件,
这次我们加上手动选择ppt文件的功能,
主要思路就是用Comdlg32库内的GetSaveFileName函数弹出文件选择框,
选择完成后获得文件路径,
通过选择的路径加载PPT文件。
功能效果
功能实现
定义OpenFileName类
这个类是固定的结构,详细说明可以查看官方文档
OpenFileName详细参数
https://docs.microsoft.com/zh-cn/windows/win32/api/commdlg/ns-commdlg-openfilenamea?redirectedfrom=MSDN
主要用于GetSaveFileName的传参。
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
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;
}
定SelectFileDialog类
这个类主要定义了系统的函数。
using System.Runtime.InteropServices;
public class SelectFileDialog
{
//系统函数
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
public static bool GetSFN([In, Out] OpenFileName ofn)
{
return GetSaveFileName(ofn);
}
}
选择文件
新建OpenFileName对象并进行赋值。
public void SelectFile() {
OpenFileName file = new OpenFileName();
file.structSize = Marshal.SizeOf(file);
file.filter = "文件(*.ppt;*.pptx)\0*.ppt;*.pptx";
file.file = new string(new char[256]);
file.maxFile = file.file.Length;
file.fileTitle = new string(new char[64]);
file.maxFileTitle = file.fileTitle.Length;
file.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
file.title = "选择文件";
file.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
if (SelectFileDialog.GetSaveFileName(file))
{
LoadPPTFile(file.file);
}
}
这里的filter属性定义了选择的范围必须是“ppt”和“pptx”后缀文件。
file.filter = "文件(*.ppt;*.pptx)\0*.ppt;*.pptx";