您当前的位置: 首页 >  unity

十幺卜入

暂无认证

  • 3浏览

    0关注

    119博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d实现手动选择动态加载PPT文件并展示

十幺卜入 发布时间:2021-03-23 13:21:04 ,浏览量:3

Unity3d实现手动选择动态加载PPT文件并展示
  • 前言
  • 功能效果
  • 功能实现
    • 定义OpenFileName类
    • 定SelectFileDialog类
    • 选择文件

前言

上一篇文章我讲了Unity3d实现加载PPT文件并展示,其中已知问题也在上一篇中解决。没看的自行前往查看: Unity3d实现加载PPT文件并展示 之前实现的是加载固定的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";

关注
打赏
1663314737
查看更多评论
立即登录/注册

微信扫码登录

0.1889s