◄ Unity 『功能总结』►——调用FileDialog(window弹框/调取本地文件)

调用FileDialog(window弹框/调取本地文件)

首先写好Win32调用类

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    #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


    #region Win32API WRAP
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    static extern bool GetOpenFileName([In, Out] LocalDialog dialog);  //这个方法名称必须为GetOpenFileName
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    static extern bool GetSaveFileName([In, Out] LocalDialog dialog);  //这个方法名称必须为GetSaveFileName
    #endregion
}

public class LocalDialog
{
    //链接指定系统函数       打开文件对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    public static bool GetOFN([In, Out] OpenFileName ofn)
    {
        return GetOpenFileName(ofn);
    }

    //链接指定系统函数        另存为对话框
    [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);
    }

    //窗口置顶
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

}


调用方法 ( ofn.filter 是你可选择文件格式过滤文件格式/可以看见我这写的部常见的图片文件格式)

    void OpenFileDownLoadFunction()
    {
        OpenFileName ofn = new OpenFileName();

        ofn.structSize = Marshal.SizeOf(ofn);

        ofn.filter = "所有图片格式(.bmp;.jpeg;.jpg;.png;)\0*.bmp;*.jpeg;*.jpg;*.png;";

        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 = UnityEngine.Application.dataPath;//默认路径

        ofn.title = "选择你的的图片:";

        //ofn.defExt = "JPG";//显示文件的类型
        
        //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR

        ofn.dlgOwner = LocalDialog.GetForegroundWindow(); //这一步将文件选择窗口置顶。

        if (LocalDialog.GetSaveFileName(ofn))
        {
            print(ofn.file);
            print(ofn.title);
        }

    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity WebGL应用程序中,由于浏览器的安全性限制,它无法直接访问用户的本地文件系统。因此,如果您需要读取用户的本地文件,您需要让用户手动选择文件并上传到您的服务器,然后从服务器端读取该文件。 但是,如果您只是需要让用户选择文件并在WebGL应用程序中加载该文件,您可以使用HTML5的File API和Unity的WWW类来实现。具体步骤如下: 1. 在HTML页面中创建一个文件输入框,并在用户选择文件后触发一个回调函数。 ```html <input type="file" id="fileInput" onchange="loadFile()" /> ``` 2. 在回调函数中使用File API读取用户选择的文件,将文件数据传递给Unity的JavaScript代码。 ```javascript function loadFile() { var fileInput = document.getElementById("fileInput"); var file = fileInput.files[0]; var reader = new FileReader(); reader.onload = function (e) { var fileData = e.target.result; unityInstance.SendMessage("YourGameObjectName", "LoadFile", fileData); }; reader.readAsArrayBuffer(file); } ``` 3. 在Unity的JavaScript代码中,使用WWW类加载从HTML页面中传递过来的文件数据。 ```javascript function LoadFile(fileData) { var url = URL.createObjectURL(new Blob([fileData])); var www = new WWW(url); // Do something with the loaded file } ``` 需要注意的是,由于加载本地文件是一个异步操作,因此您需要在代码中处理异步加载的情况。另外,由于这种方法需要上传文件到服务器,因此对于大型文件或需要频繁读写的文件,可能不是一个有效的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臭臭~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值