Unity调用Window窗口


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

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

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


Chinar —— 心分享、心创新!

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

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

Chinar 教程效果:



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


1

File Controller —— 文件控制脚本

晚会儿修改



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

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

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

即可正常调用

具体需求,需要自己来定,保存到某个路径下

  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. /// <summary>
  5. /// 文件控制脚本
  6. /// </summary>
  7. public class ChinarFileController : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// 打开项目
  11. /// </summary>
  12. public void OpenProject()
  13. {
  14. OpenFileDlg pth = new OpenFileDlg();
  15. pth.structSize = Marshal.SizeOf(pth);
  16. pth.filter = "All files (*.*)|*.*";
  17. pth.file = new string(new char[256]);
  18. pth.maxFile = pth.file.Length;
  19. pth.fileTitle = new string(new char[64]);
  20. pth.maxFileTitle = pth.fileTitle.Length;
  21. pth.initialDir = Application.dataPath.Replace("/", "\\") + "\\Resources"; //默认路径
  22. pth.title = "打开项目";
  23. pth.defExt = "dat";
  24. pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
  25. if (OpenFileDialog.GetOpenFileName(pth))
  26. {
  27. string filepath = pth.file; //选择的文件路径;
  28. Debug.Log(filepath);
  29. }
  30. }
  31. /// <summary>
  32. /// 保存文件项目
  33. /// </summary>
  34. public void SaveProject()
  35. {
  36. SaveFileDlg pth = new SaveFileDlg();
  37. pth.structSize = Marshal.SizeOf(pth);
  38. pth.filter = "All files (*.*)|*.*";
  39. pth.file = new string(new char[256]);
  40. pth.maxFile = pth.file.Length;
  41. pth.fileTitle = new string(new char[64]);
  42. pth.maxFileTitle = pth.fileTitle.Length;
  43. pth.initialDir = Application.dataPath; //默认路径
  44. pth.title = "保存项目";
  45. pth.defExt = "dat";
  46. pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
  47. if (SaveFileDialog.GetSaveFileName(pth))
  48. {
  49. string filepath = pth.file; //选择的文件路径;
  50. Debug.Log(filepath);
  51. }
  52. }
  53. }

2

FileDlog —— 文件日志

无需挂载到空物体上

  1. using System.Runtime.InteropServices;
  2. using System;
  3. /// <summary>
  4. /// 文件日志类
  5. /// </summary>
  6. // [特性(布局种类.有序,字符集=字符集.自动)]
  7. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  8. public class ChinarFileDlog
  9. {
  10. public int structSize = 0;
  11. public IntPtr dlgOwner = IntPtr.Zero;
  12. public IntPtr instance = IntPtr.Zero;
  13. public String filter = null;
  14. public String customFilter = null;
  15. public int maxCustFilter = 0;
  16. public int filterIndex = 0;
  17. public String file = null;
  18. public int maxFile = 0;
  19. public String fileTitle = null;
  20. public int maxFileTitle = 0;
  21. public String initialDir = null;
  22. public String title = null;
  23. public int flags = 0;
  24. public short fileOffset = 0;
  25. public short fileExtension = 0;
  26. public String defExt = null;
  27. public IntPtr custData = IntPtr.Zero;
  28. public IntPtr hook = IntPtr.Zero;
  29. public String templateName = null;
  30. public IntPtr reservedPtr = IntPtr.Zero;
  31. public int reservedInt = 0;
  32. public int flagsEx = 0;
  33. }
  34. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  35. public class OpenFileDlg : ChinarFileDlog
  36. {
  37. }
  38. public class OpenFileDialog
  39. {
  40. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  41. public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
  42. }
  43. public class SaveFileDialog
  44. {
  45. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  46. public static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd);
  47. }
  48. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  49. public class SaveFileDlg : ChinarFileDlog
  50. {
  51. }

运行效果:


支持

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

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

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

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

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



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



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



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



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




" role="presentation">

技术交流群:806091680 ! Chinar 欢迎你的加入


END

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


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


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

unity 实现调用Windows窗口/对话框交互的更多相关文章

  1. Unity中调用Windows窗口选择文件

    1.OpenFileName数据接收类,如下: using UnityEngine; using System.Collections; using System; using System.Runt ...

  2. Unity中调用Windows窗口句柄以及根据需求设置并且解决扩展屏窗体显示错乱/位置错误的Bug

    问题背景: 现在在搞PC端应用开发,我们开发中需要调用系统的窗口以及需要最大化最小化,缩放窗口拖拽窗口,以及设置窗口位置,去边框等功能 解决根据: 使用user32.dll解决 具体功能: Unity ...

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

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

  4. java使用RunTime调用windows命令行

    当Java需要调用windows系统进行交互时,可以使用Runtime进行操作. 例子: 1.调用window中获取关于java相关的进行信息 Runtime rt = Runtime.getRunt ...

  5. Unity调用Windows弹框、提示框(确认与否,中文)

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

  6. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  7. C#在父窗口中调用子窗口的过程(无法访问已释放的对象)异常,不存在从对象类型System.Windows.Forms.DateTimePicker到已知的托管提供程序本机类型的映射。

    一:C#在父窗口中调用子窗口的过程(无法访问已释放的对象)异常 其实,这个问题与C#的垃圾回收有关.垃圾回收器管 理所有的托管对象,所有需要托管数据的.NET语言(包括 C#)都受运行库的 垃圾回收器 ...

  8. windows下C语言调用系统文件选择对话框

    代码片段,在windows下用C语言调用文件选择对话框,以备忘 #define DEFAULT_DIR "" char extraction_path[MAX_PATH] = DE ...

  9. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

随机推荐

  1. AAC ADTS格式分析

    转自: https://blog.csdn.net/jay100500/article/details/52955232 https://blog.csdn.net/andyhuabing/artic ...

  2. activity和fragment之前运行的生命周期

    1.activity生命周期 2.fragment的生命周期 3.对比图 4.测试代码 package com.goso.testapp; import android.app.Activity; i ...

  3. vm安装diagram

        xxx1234ZZ   xxx1234ZZ@    

  4. ResouceUtils.getFile()取不到Jar中资源文件源码小结

    Spring提供了一个工具类可以加载classpath下的文件,一般情况下无任何问题,但是当它作为公共的jar包中的工具来加载jar包中的文件时则报出找不到文件的错误. 点开看了一下这个工具类Reso ...

  5. linux下find命令详解

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; find命令的参数 ...

  6. 学习笔记-AngularJs(三)

    学习笔记-AngularJs(二)写了个所有程序语言入门时都必须要写的Hello World,那么从现在开始做那个之前说过的互联网大佬介绍的学习例子,当然这里开始会慢慢按照之前说过的目录来搭建这个学习 ...

  7. TCP_NODELAY算法使用事项

    当有一个TCP数据段不足MSS,比如要发送700Byte数据,MSS为1460Byte的情况.nagle算法会延迟这个数据段的发送,等待,直到有足够的数据填充成一个完整数据段.也许有人会问,这有什么影 ...

  8. UVa 11384 - Help is needed for Dexter 分析, 树状数组 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. [Linux]Linux下Apache服务器配置

    Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm                 //主程序包 httpd-devel-2.2.3-29.e15.i ...

  10. 7.4 C++标准模板库(STL)的概念

    参考:http://www.weixueyuan.net/view/6401.html 总结:  标准模板库为C++提供了完善的数据结构及算法. 标准模板库包括三部分:容器.算法和迭代器. 容器是对象 ...