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

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.xdnf.cn/news/5082.html

相关文章:

  • 基于nodejs + Koa +Nuxt3的订单系统项目实战
  • 应急响应靶机训练-挖矿事件:知攻善防实验室
  • element-ui分页的使用及修改样式
  • RabbitMQ事务机制
  • NextPolish1.4.1 安装与使用-bioinformatics tools54
  • leader-line文本添加click点击事件
  • 【人工智能学习之注意力机制浅析】
  • 学习黑客威胁情报(Threat Intelligence)
  • 一文了解Python中的requests库:网络交互的基础
  • AI服务器通常会运用在哪些场景当中?
  • STM32CubeMX安装及使用分享
  • 切比雪夫不等式专题习题
  • Qt开发:项目视图(Item Views)的介绍和使用
  • CRC 循环冗余校验
  • TRAE 配置blender MCP AI自动3D建模
  • 京东商品详情接口 item_get 深度解析
  • 学习一下linux的思路和笔记分享
  • CSS opacity
  • 已解决:安卓刚打开新项目的时候,会下载该项目要求的sdk gradle,开了科学上网也慢
  • C++学习-入门到精通-【5】类模板array和vector、异常捕获
  • B树和B+树
  • xxl-job的pg数据库改造适配
  • TiDB预研-分页查询、连接查询、执行计划
  • yolov5基础--yolov5源码阅读(common.py)
  • LeRobot 项目部署运行逻辑(六)——visualize_dataset_html.py/visualize_dataset.py
  • 4-37:某单位分配到一个地址块 136.23.12.64/26。现在需要进一步划分为4个一样大的子网。试问:....
  • 全局异常未能正确捕获到对应的异常
  • shell脚本基础详细学习(更新中)
  • 资产月报怎么填?资产月报填报指南
  • 半导体供应链集成使用EDI,RosettaNet,及自定义API 之间的差异