当前位置: 首页 > news >正文

Unity 点击按钮,打开 Windows 文件选择框,并加载图片

代码如下:

using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.UI;/// <summary>
/// 文件日志类
/// </summary>
// [特性(布局种类.有序,字符集=字符集.自动)]
[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 OpenFileName(){this.structSize = Marshal.SizeOf(this);this.filter = "Texure Files(*图片文件)\0*.png;*.jpg;*.bmp;*.jpeg\0";this.file = new string(new char[256]);this.maxFile = this.file.Length;this.fileTitle = new string(new char[64]);this.maxFileTitle = this.fileTitle.Length;this.initialDir = "C:\\"; //默认路径this.title = "打开项目";this.defExt = "JPG";this.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;}
}public class OpenFileHandle : MonoBehaviour
{public Button BtnOpenFile;public Image showImg;//打开文件[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]public static extern bool GetOpenFileName([In, Out] OpenFileName ofd);void Start(){BtnOpenFile.onClick.AddListener(BtnOpenFileEvent);}void BtnOpenFileEvent(){OpenFileName path = new OpenFileName();if (GetOpenFileName(path)){string filepath = path.file;StartCoroutine(ReadTexture(filepath, LoadImg));}}/// <summary>/// 读取图片/// </summary>/// <param name="path"></param>/// <param name="action"></param>/// <returns></returns>IEnumerator ReadTexture(string path, UnityAction<Texture> action){Debug.Log(path);UnityWebRequest request = UnityWebRequestTexture.GetTexture(path);yield return request.SendWebRequest();if (request.isNetworkError || request.isHttpError){Debug.Log(request.error);}else{byte[] imgdata = request.downloadHandler.data;action(DownloadHandlerTexture.GetContent(request));}}/// <summary>/// 加载图片/// </summary>/// <param name="texture"></param>void LoadImg(Texture texture){Sprite ImgSprite = Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));showImg.sprite = ImgSprite;}
}

实现效果如下:

http://www.dtcms.com/a/179939.html

相关文章:

  • iOS创建Certificate证书、制作p12证书流程
  • Jsoup与HtmlUnit:两大Java爬虫工具对比解析
  • LeRobot 项目部署运行逻辑(五)——intelrealsense.py/configs.py
  • -bash: /usr/local/mysql/bin/mysqld: No such file or directory
  • uni-app 中的条件编译与跨端兼容
  • Windows 11家庭中文版Docker Desktop汉化全攻略
  • 【Ansible】模块详解
  • Android 项目中配置了多个 maven 仓库,但依赖还是下载失败,除了使用代理,还有其他方法吗?
  • MATLAB制作柱状图与条图:数据可视化的基础利器
  • [6-1] TIM定时中断 江协科技学习笔记(45个知识点)
  • React 第三十七节 Router 中 useOutlet Hook的使用介绍以及注意事项
  • [计算机科学#13]:算法
  • StreamRL:弹性、可扩展、异构的RLHF架构
  • 数据结构 集合类与复杂度
  • 6.01 Python中打开usb相机并进行显示
  • 【Cesium入门教程】第五课:数据源
  • 敦普水性无铬锌铝涂层:汽车紧固件防锈15年,解决螺栓氢脆腐蚀双痛点
  • 在企业级项目中高效使用 Maven-mvnd
  • 量化学习DAY2-开始批量提交alpha!
  • 【Linux】自定义shell的编写
  • 【IDEA_Maven】(进阶版)永久性的更改IDEA中每个项目所依赖的Maven默认配置文件及其仓库路径
  • 【Python 元组】
  • 网上商城系统
  • Kafka集群加入新Broker节点会发生什么
  • SQLite 转换为 MySQL 数据库
  • Go语言中 源文件开头的 // +build 注释的用法
  • LeetCode难题解析:数字字符串的平衡排列数目
  • 力扣:轮转数组
  • Python字典:数据操作的核心容器
  • .Net HttpClient 概述