Unity 3D调用Windows打开、保存窗口、文件浏览器

简介: Unity调用Window窗口本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar ——...

Unity调用Window窗口


本文提供全流程,中文翻译。

Chinar 坚持将简单的生活方式,带给世人!

(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例)



Chinar —— 心分享、心创新!

助力快速完成 Unity调用 Window api 执行保存/打开操作

为新手节省宝贵的时间,避免采坑!


Chinar 教程效果:
这里写图片描述



全文高清图片,点击即可放大观看 (很多人竟然不知道)


1

File Controller —— 文件控制脚本


用来控制打开/保存项目文件

将两个脚本放到项目中,分别绑定2个按钮 打开/保存

ChinarFileController 脚本需要挂载到空物体上

即可正常调用

具体需求,需要自己来定,保存到某个路径下
举个栗子黑白88

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


/// <summary>
/// 文件控制脚本
/// </summary>
public class ChinarFileController : MonoBehaviour
{
    /// <summary>
    /// 打开项目
    /// </summary>
    public void OpenProject()
    {
        OpenFileDlg pth  = new OpenFileDlg();
        pth.structSize   = Marshal.SizeOf(pth);
        pth.filter       = "All files (*.*)|*.*";
        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.Replace("/", "\\") + "\\Resources"; //默认路径
        pth.title        = "打开项目";
        pth.defExt       = "dat";
        pth.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetOpenFileName(pth))
        {
            string filepath = pth.file; //选择的文件路径;  
            Debug.Log(filepath);
        }
    }


    /// <summary>
    /// 保存文件项目
    /// </summary>
    public void SaveProject()
    {
        SaveFileDlg pth  = new SaveFileDlg();
        pth.structSize   = Marshal.SizeOf(pth);
        pth.filter       = "All files (*.*)|*.*";
        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; //默认路径
        pth.title        = "保存项目";
        pth.defExt       = "dat";
        pth.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (SaveFileDialog.GetSaveFileName(pth))
        {
            string filepath = pth.file; //选择的文件路径;  
            Debug.Log(filepath);
        }
    }
}
AI 代码解读

2

FileDlog —— 文件日志


无需挂载到空物体上
举个栗子黑白88

using System.Runtime.InteropServices;
using System;


/// <summary>
/// 文件日志类
/// </summary>
// [特性(布局种类.有序,字符集=字符集.自动)]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class ChinarFileDlog
{
    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 : ChinarFileDlog
{
}

public class OpenFileDialog
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
}

public class SaveFileDialog
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd);
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SaveFileDlg : ChinarFileDlog
{
}
AI 代码解读

运行效果:
这里写图片描述


支持

May Be —— 搞开发,总有一天要做的事!


拥有自己的服务器,无需再找攻略!

Chinar 提供一站式教程,闭眼式创建!

为新手节省宝贵时间,避免采坑!


先点击领取 —— 阿里全产品优惠券 (享受最低优惠)


1 —— 云服务器超全购买流程 (新手必备!)

2 —— 阿里ECS云服务器自定义配置 - 购买教程(新手必备!)

3—— Windows 服务器配置、运行、建站一条龙 !

4 —— Linux 服务器配置、运行、建站一条龙 !





Chinar


END

本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究

对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com

对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址

目录
打赏
0
0
0
0
32
分享
相关文章
Unity调用Windows弹出确认框
在 Unity 中调用 Windows 弹出确认框,可通过 Windows API 或 .NET 框架实现。使用 Windows API 的方式是通过 P/Invoke 技术调用 MessageBox 函数,创建模态对话框。代码示例展示了如何在应用退出时弹出确认框,用户选择“确定”则退出游戏。此方法也适用于 ALT+F4 触发的退出确认。
|
9月前
|
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
394 0
WIN11 Chrome 双击打不开闪退及Chrome浏览器不能拖拽文件crx
【11月更文挑战第6天】本文介绍了 WIN11 系统中 Chrome 浏览器双击打不开闪退及不能拖拽文件 crx 的原因和解决方法。包括浏览器版本过旧、扩展程序冲突、硬件加速问题、缓存过多、安全软件冲突、系统文件损坏、用户配置文件损坏等问题的解决方案,以及 crx 文件的屏蔽、权限问题和文件格式问题的处理方法。
730 2
java中post请求调用下载文件接口浏览器未弹窗而是返回一堆json,为啥
客户端调接口需要返回另存为弹窗,下载文件,但是遇到的问题是接口调用成功且不报错,浏览器F12查看居然返回一堆json,而没有另存为弹窗; > 正确的效果应该是:接口调用成功且浏览器F12不返回任何json,而是弹窗另存为窗口,直接保存文件即可。
285 2
基于postMessage和BroadcastChannel实现浏览器跨Tab窗口通信的方法介绍
基于postMessage和BroadcastChannel实现浏览器跨Tab窗口通信的方法介绍
216 0
|
7月前
|
Windows之窗口原理
这篇文章主要介绍了Windows窗口原理和如何使用Windows API创建和管理窗口。
126 0
roogoo发布的quartz32.dll让浏览器不断弹出广告窗口
roogoo发布的quartz32.dll让浏览器不断弹出广告窗口
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
【Azure Developer】浏览器查看本地数据文件时遇见跨域问题(CORS)
穿越数字洪流,揭秘Unity3d中的视频魔法!Windows、Android和iOS如何征服RTSP与RTMP的终极指南!
【8月更文挑战第15天】在数字媒体的海洋中,实时视频流是连接世界的桥梁。对于那些渴望在Unity3d中搭建这座桥梁的开发者来说,本文将揭示如何在Windows、Android和iOS平台上征服RTSP与RTMP的秘密。我们将深入探讨这两种协议的特性,以及在不同平台上实现流畅播放的技巧。无论你是追求稳定性的RTSP拥趸,还是低延迟的RTMP忠实粉丝,这里都有你需要的答案。让我们一起穿越数字洪流,探索Unity3d中视频魔法的世界吧!
192 2
快速参考:用C# Selenium实现浏览器窗口缩放的步骤
在C#结合Selenium的网络爬虫应用中,掌握浏览器窗口缩放、代理IP、cookie与user-agent设置至关重要。本文详述了如何配置代理(如亿牛云加强版),自定义用户代理,启动ChromeDriver,并访问目标网站如抖音。通过执行JavaScript代码实现页面缩放至75%,并添加cookie增强匿名性。此策略有效规避反爬机制,提升数据抓取的准确度与范围。代码示例展示了整个流程,确保爬虫操作的灵活性与高效性。
181 3

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等