Unity C# 打开windows对话框选择文件夹或选择文件

unity没有提供打开windows对话框的api,在开发种也会遇到选择系统文件夹或选择系统文件的需求

///
/工具:windows系统文件夹/文件选择窗口//
///
using System;
using System.Runtime.InteropServices;
public class OpenFile
{/// <summary>/// 选择文件夹/// </summary>public static string ChooseWinFolder(){//使用如下:OpenDialogDir ofn = new OpenDialogDir();ofn.pszDisplayName = new string(new char[2000]); ;     // 存放目录路径缓冲区  ofn.title = "选择文件夹";// 标题  //ofn.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框  IntPtr pidlPtr = WindowDll.SHBrowseForFolder(ofn);char[] charArray = new char[2000];for (int i = 0; i < 2000; i++)charArray[i] = '\0';WindowDll.SHGetPathFromIDList(pidlPtr, charArray);string fullDirPath = new String(charArray);return fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));//fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));//Debug.Log(fullDirPath);//这个就是选择的目录路径}/// <summary>/// 选择文件/// </summary>public static string ChooseWinFile(){OpenFileName OpenFileName = new OpenFileName();OpenFileName.structSize = Marshal.SizeOf(OpenFileName);OpenFileName.filter = "应用程序(*.exe)\0*.exe";OpenFileName.file = new string(new char[1024]);OpenFileName.maxFile = OpenFileName.file.Length;OpenFileName.fileTitle = new string(new char[64]);OpenFileName.maxFileTitle = OpenFileName.fileTitle.Length;//OpenFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');//默认路径OpenFileName.title = "选择exe文件";OpenFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;if (WindowDll.GetOpenFileName(OpenFileName))return OpenFileName.file;elsereturn null;}
}/// <summary>
/// windows系统文件选择窗口
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFileName
{public int structSize;public IntPtr dlgOwner;public IntPtr instance;public String filter;public String customFilter;public int maxCustFilter;public int filterIndex;public String file;public int maxFile;public String fileTitle;public int maxFileTitle;public String initialDir;public String title;public int flags;public short fileOffset;public short fileExtension;public String defExt;public IntPtr custData;public IntPtr hook;public String templateName;public IntPtr reservedPtr;public int reservedInt;public int flagsEx;
}/// <summary>
/// windows系统文件夹选择窗口
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenDialogDir
{public IntPtr hwndOwner;public IntPtr pidlRoot;public String pszDisplayName;public String title;public UInt32 ulFlags;public IntPtr lpfno;public IntPtr lParam;public int iImage;
}
public class WindowDll
{//链接指定系统函数       打开文件对话框[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("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/143636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SPringBoot项目调用本地python算法

在Spring Boot项目中调用本地Python算法的方法通常是通过使用Spring的Java-Python交互功能&#xff0c;以及通过Spring的依赖注入将Python函数注入到Java对象中。下面是一种可能的方法&#xff1a; 首先&#xff0c;你需要在你的Spring Boot项目中配置Python解释器。你可以使用…

轻量封装WebGPU渲染系统示例<32>- 若干线框对象(源码)

当前示例源码github地址: https://github.com/vilyLei/voxwebgpu/blob/feature/rendering/src/voxgpu/sample/WireframeEntityTest.ts 当前示例运行效果: 此示例基于此渲染系统实现&#xff0c;当前示例TypeScript源码如下: export class WireframeEntityTest {private mRsc…

人工智能基础_机器学习030_ElasticNet弹性网络_弹性回归的使用---人工智能工作笔记0070

然后我们再来看elastic-net弹性网络,之所以叫弹性是因为,他融合了L1和L2正则,可以看到 他的公式 公式中有L1正则和L2正则两个都在这个公式中 可以看到弹性网络,在很多特征互相联系的时候,非常有用,比如, 相关性,如果数学好,那么物理也好,如果语文好,那么英语也好 这种联系 正…

JZ22:链表中倒数第k个结点

JZ22&#xff1a;链表中倒数第k个结点 题目描述&#xff1a; 输入一个链表&#xff0c;输出该链表中倒数第k个结点。 示例1 输入&#xff1a; 1,{1,2,3,4,5} 返回值&#xff1a; {5} 分析&#xff1a; 快慢指针思想&#xff1a; 需要两个指针&#xff0c;快指针fast&…

python 基础语法 (常常容易漏掉)

同一行显示多条语句 python语法中要求缩进&#xff0c;但是同一行可以显示多条语句 在 Python 中&#xff0c;可以使用分号 (;) 将多个语句放在同一行上。这样可以在一行代码中执行多个语句&#xff0c;但需要注意代码的可读性和维护性。 x 5; y 10; z x y; print(z) 在…

使用c++程序,实现图像平移变换,图像缩放、图像裁剪、图像对角线镜像以及图像的旋转

数字图像处理–实验三A图像的基本变换 实验内容 A实验&#xff1a; &#xff08;1&#xff09;使用VC设计程序&#xff1a;实现图像平移变换&#xff0c;图像缩放、图像裁剪、图像对角线镜像。 &#xff08;2&#xff09;使用VC设计程序&#xff1a;对一幅高度与宽度均相等的…

PaddleClas学习1——使用PPLCNet模型对车辆属性进行识别(python)

使用PPLCNet模型对车辆属性进行识别 1. 配置PaddlePaddle,PaddleClas环境1.1 安装PaddlePaddle(1)创建 docker 容器(2)退出/进入 docker 容器(3) 安装验证1.2 安装python3.8(可选)1.3 安装 PaddleClas2. 模型推理2.1 下载官方提供的车辆属性模型2.2 基于 Python 预测引…

linux 系统下文本编辑常用的命令

一、是什么 Vim是从 vi 发展出来的一个文本编辑器&#xff0c;代码补全、编译及错误跳转等方便编程的功能特别丰富&#xff0c;在程序员中被广泛使用。 简单的来说&#xff0c; vi 是老式的字处理器&#xff0c;不过功能已经很齐全了&#xff0c;但是还是有可以进步的地方 而…

Playwright UI 自动化测试实战

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…

Kylin-Server-V10-SP3+Gbase+宝兰德信创环境搭建

目录 一、Kylin-Server-V10-SP3 安装1.官网下载安装包2.创建 VMware ESXi 虚拟机3.加载镜像&#xff0c;安装系统 二、Gbase 安装1.下载 Gbase 安装包2.创建组和用户、设置密码3.创建目录4.解压包5.安装6.创建实例7.登录8.常见问题 三、宝兰德安装1.获取安装包2.解压安装3.启动…

参考意义大。4+巨噬细胞相关生信思路,简单易复现。

今天给同学们分享一篇生信文章“Angiogenesis regulators S100A4, SPARC and SPP1 correlate with macrophage infiltration and are prognostic biomarkers in colon and rectal cancers”&#xff0c;这篇文章发表在Front Oncol期刊上&#xff0c;影响因子为4.7。 结果解读&a…

Java Stream 的常用API

Java Stream 的常用API 遍历&#xff08;forEach&#xff09; package com.liudashuai;import java.util.ArrayList; import java.util.List;public class Test {public static void main(String[] args) {List<Person> userList new ArrayList<>();userList.ad…

杭电oj 2035 人见人爱A^B C语言

#include<stdio.h>void main() {int a, b, i,num;while (~scanf_s("%d%d", &a, &b) && (a ! 0 || b ! 0)){num a;for (i 1; i < b; i){num * a;num % 1000;}printf("%d\n", num);} }

Clickhouse学习笔记

学习内容参考&#xff1a;一套上手ClickHouse-OLAP分析引擎&#xff0c;囊括Prometheus与Grafana_哔哩哔哩_bilibili 下为笔记链接&#xff0c;以及全套笔记pdf版本 Clickhouse学习笔记&#xff08;1&#xff09;—— ClickHouse的安装启动_clickhouse后台启动_THE WHY的博客-C…

使用Filebeat+Kafka+Logstash+Elasticsearch构建日志分析系统

随着时间的积累&#xff0c;日志数据会越来越多&#xff0c;当您需要查看并分析庞杂的日志数据时&#xff0c;可通过FilebeatKafkaLogstashElasticsearch采集日志数据到Elasticsearch中&#xff0c;并通过Kibana进行可视化展示与分析。本文介绍具体的实现方法。 一、背景信息 …

C语言不可不敲系列:跳水比赛排名问题

目录 1题干&#xff1a; 2解题思路&#xff1a; 3代码: 4运行结果: 5总结: 1题干&#xff1a; 5位运动员参加了10米台跳水比赛&#xff0c;有人让他们预测比赛结果 A选手说&#xff1a;B第二&#xff0c;我第三&#xff1b; B选手说&#xff1a;我第二&#xff0c;E第四&am…

【LeetCode刷题-滑动窗口】--1456.定长子串中元音的最大数目

1456.定长子串中元音的最大数目 方法&#xff1a;使用滑动窗口 class Solution {public int maxVowels(String s, int k) {int n s.length();int sum 0;for(int i 0;i<k;i){sum isVowel(s.charAt(i));}int ans sum;for(int i k;i<n;i){sum sum isVowel(s.charAt…

MHA实验和架构

什么是MHA&#xff1f; masterhight availabulity&#xff1a;基于主库的高可用环境下可以实现主从复制、故障切换 MHA的主从架构最少要一主两从 MHA的出现是为了解决MySQL的单点故障问题。一旦主库崩溃&#xff0c;MHA可以在0-30秒内自动完成故障切换。 MHA的数据流向和工…

钡铼技术4G RTU采集器在智慧农业灌溉控制中的使用介绍

随着科技的不断发展&#xff0c;智慧农业已经成为现代农业发展的重要方向之一。在智慧农业中&#xff0c;灌溉控制是至关重要的环节&#xff0c;而4G RTU采集器作为一种先进的数据采集设备&#xff0c;为智慧农业灌溉控制提供了全新的解决方案。本文将就钡铼技术有限公司的4G R…