unity 打开文件资源管理器(不使用UnityEditor)

问题描述:
有个地方需要用户上传一张图片,网上查了,使用了如下代码:

string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");

然后在打包发布时遇到问题,因为EditorUtility使用了UnityEditor,无法发布,无奈只能另寻解决方法

解决方案:
新建一个脚本文件将下面的代码粘进去,静态加载即可

using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;/// <summary>
/// 调用系统代码,打开本地文件夹
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenDialogueFile
{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 OpenDialogDir
{public IntPtr hwndOwner = IntPtr.Zero;public IntPtr pidlRoot = IntPtr.Zero;public String pszDisplayName = null;public String lpszTitle = null;public UInt32 ulFlags = 0;public IntPtr lpfn = IntPtr.Zero;public IntPtr lParam = IntPtr.Zero;public int iImage = 0;
}[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;
}public static class FolderBrowserHelper
{#region Window[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetOpenFileName([In, Out] OpenDialogueFile ofn);[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool GetSaveFileName([In, Out] OpenDialogueFile ofn);[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);[DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);#endregionpublic const string IMAGEFILTER = "图片文件(*.jpg;*.png)\0*.jpg;*.png";public const string ALLFILTER = "所有文件(*.*)\0*.*";/// <summary>/// 选择文件/// </summary>/// <param name="callback">返回选择文件夹的路径</param>/// <param name="filter">文件类型筛选器</param>public static string SelectFile(string filter = ALLFILTER){try{OpenFileName openFileName = new OpenFileName();openFileName.structSize = Marshal.SizeOf(openFileName);// openFileName.initialDir = "初始目录"openFileName.filter = filter;openFileName.file = new string(new char[256]);openFileName.maxFile = openFileName.file.Length;openFileName.fileTitle = new string(new char[64]);openFileName.maxFileTitle = openFileName.fileTitle.Length;openFileName.title = "选择文件";openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;if (GetSaveFileName(openFileName)){string filepath = openFileName.file; //选择的文件路径;  if (File.Exists(filepath)){return filepath;}}}catch (Exception e){Debug.LogError(e);}return null;}}

调用方法如下:

string imagePath = FolderBrowserHelper.SelectFile();

这样可以正常替代原本的

string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Go高级库存照片源码v5.3

GoStock – 免费和付费库存照片脚本这是一个免费和付费共享高质量库存照片的平台,用户可以上传照片与整个社区和访客分享,并可以通过 PayPal 接收捐款。此外,用户还可以点赞、评论、分享和收藏您最喜欢的照片。 下载 特征: 使用Laravel 10构建订阅系统Stripe 连接渐进式网页…

【Python机器学习】模型评估与改进——分层k折交叉验证

在k折分层验证中&#xff0c;将数据集划分为k折时&#xff0c;从数据的前k分之一开始划分&#xff0c;这可能并不总是一个好主意&#xff0c;例如iris数据集中&#xff1a; from sklearn.datasets import load_irisirisload_iris() print(Iris labels:\n:{}.format(iris.targe…

2.Python学习:数据类型和变量

1.标识符命名规则 只能由数字、字母、下划线组成不能以数字开头不能是关键字&#xff08;如class等python内部已经使用的标识符&#xff09;区分大小写 查看关键字&#xff1a; print(keyword.kwlist)2.数据类型 2.1常见数据类型 2.1.1Number数值型&#xff1a; 整数int&a…

13 - Python网络编程入门

网络编程入门 计算机网络基础 计算机网络是独立自主的计算机互联而成的系统的总称&#xff0c;组建计算机网络最主要的目的是实现多台计算机之间的通信和资源共享。今天计算机网络中的设备和计算机网络的用户已经多得不可计数&#xff0c;而计算机网络也可以称得上是一个“复…

自定义isdate函数,判定日期字符串有效性

自定义isdate函数&#xff0c;按日期“属性”&#xff0c;判定字符串日期有效性。 (笔记模板由python脚本于2024年07月05日 15:28:04创建&#xff0c;本篇笔记适合喜欢探究python内建模块的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&#xff1a;https://www.python.o…

One day for Chinese families

周围生活中的普通家庭的一天流程&#xff1a; 【上班的一天】 【放假的一天】 有家庭的人&#xff0c;上班流程&#xff1a; 01&#xff09;准备早餐&#xff0c;牛奶&#xff0c;面包 02&#xff09;叫娃娃起床&#xff0c;一般要蛮久的&#xff1b;沟通交流 -- 哄娃娃 -- 生气…

2-5 softmax 回归的简洁实现

我们发现通过深度学习框架的高级API能够使实现线性回归变得更加容易。 同样&#xff0c;通过深度学习框架的高级API也能更方便地实现softmax回归模型。 本节如在上节中一样&#xff0c; 继续使用Fashion-MNIST数据集&#xff0c;并保持批量大小为256。 import torch from torc…

Linux操作系统的引导过程

系统初始化进程与文件、systemd概述、单元类型、切换运行级别、查看系统默认默认运行、永久切换、常见的系统服务&#xff08;centos&#xff09;-CSDN博客 centos 7系统升级内核&#xff08;ELRepo仓库&#xff09;、小版本升级、自编译内核-CSDN博客 ss命令详细使用讲解文…

微积分-导数4(三角函数的导数)

证明 f ( x ) sin ⁡ x f(x) \sin x f(x)sinx的导数为 f ′ ( x ) cos ⁡ x f(x) \cos x f′(x)cosx 已知函数 f ( x ) sin ⁡ x f(x) \sin x f(x)sinx 画出 f ( x ) f(x) f(x)图像以及 f ′ ( x ) f(x) f′(x)的图像 因此&#xff0c;我们可以合理的猜测&#xff1a;…

每日一题~ (判断是否是合法的出栈序列)

大概的题意&#xff1a; 将 1-n 按照顺序进栈&#xff0c;问 输入的序列是否是合法的出栈序列。 遍历序列&#xff0c;如果当前这个值a小于 栈顶的值&#xff0c;说明它还未进栈&#xff08;因为我们是按照顺序进栈的&#xff09;&#xff0c;所以我们将 一些元素进栈&#xff…

最短路:Dijkstra

原始模板&#xff1a; 时间复杂度O() 使用于图很满的情况 struct Node{int y,v;Node(int _y,int _v){y_y;v_v;} };vector<Node> edge[N1]; int n,m,dist[N1]; bool b[N1];int Dijistra(int s,int t){memset(b,false,sizeof(b));memset(dist,127,sizeof(dist));dist[s]…

Linux开发讲课33---线程实现与线程控制步骤简析

线程概述 进程是系统中程序执行和资源分配的基本单位。 每个进程都拥有自己的数据段、代码段和堆栈段&#xff0c;这就造成了进程在进行切换等操作时都需要有比较负责的上下文切换等动作。为了进一步减少处理机的空转时间支持多处理器和减少上下文切换开销&#xff0c;进程在演…

第5章 认证授权:需求分析,Security介绍(OAuth2,JWT),用户认证,微信扫码登录,用户授权

1 模块需求分析 1.1 什么是认证授权 截至目前&#xff0c;项目已经完成了课程发布功能&#xff0c;课程发布后用户通过在线学习页面点播视频进行学习。如何去记录学生的学习过程呢&#xff1f;要想掌握学生的学习情况就需要知道用户的身份信息&#xff0c;记录哪个用户在什么…

工作手机怎么做好业务员工作微信的监控管理

什么是工作手机管理系统&#xff1f; 工作手机管理系统是专为企业管理设计的员工微信管理&#xff0c;它通过监控通讯记录、保障数据安全、自动检测敏感行为、永久保留客户信息等功能&#xff0c;帮助企业提升销售效率、维护客户资源安全&#xff0c;并确保业务流程的合规性。…

自动化设备上位机设计 三

目录 一 设计原型 二 后台源码 一 设计原型 二 后台源码 using SqlSugar;namespace 自动化上位机设计 {public partial class Form1 : Form{SqlHelper sqlHelper new SqlHelper();SqlSugarClient dbContent null;bool IsRun false;int Count 0;public Form1(){Initializ…

奇景光电战略投资Obsidian,共筑热成像技术新未来

5月29日,业界领先的IC设计公司奇景光电宣布,将对热成像传感器解决方案制造商Obsidian进行战略性投资,并以主要投资者的身份,参与到Obsidian的可转换票据融资活动中。虽然奇景光电并未公开具体的投资金额,但这一举动无疑向市场传递了一个明确的信号:奇景光电对Obsidian的技…

深度学习:为什么说英伟达A100或RTX A6000等专业GPU比RTX 4090更适合深度学习呢?

目录 一、关键术语 CUDA cores&#xff08;CUDA内核&#xff09;&#xff1a; memory bandwidth&#xff08;内存带宽&#xff09;&#xff1a; 二、深度学习的显卡硬件要求 三、NVIDIA显卡A100、RTX A6000和RTX 4090对比 1、NVIDIA A100 2、NVIDIA RTX A6000 3、NVIDI…

方法引用 异常 file

一.方法引用 1.方法引用概述 eg: 表示引用run1类里面的sxxxx方法 把这个方法当做抽象方法的方法体 &#xff1a;&#xff1a;是方法引用符 //方法引用Integer[] arr{4,3,1,6,2,7,8,5};Arrays.sort(arr,run1::subtraction);System.out.println(Arrays.toString(arr));}publi…

AI老照片生成视频

地址&#xff1a;AI老照片 让你的图片动起来, 老照片修复与动态化

HTTP-概述

概念 :Hyper Text Transfer Protocol&#xff0c;超文本传输协议&#xff0c;规定了浏览器和服务器之间数据传输的规则。 原始数据格式 特点 1. 基于TCP协议:面向连接&#xff0c;安全&#xff08;三次握手&#xff09; 2. 基于请求-响应模型的:一次请求对应一次响应&#xf…