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

unity显示图片

目录

创建c#脚本

自己创建gui组件:

入门教程:

读取图片:

Unity读取图片并显示到UI中

显示双目相机,可以跑通

unity3d显示图片


参考教程,GameObject

Unity UGUI的Image(图片)组件的介绍及使用 - 简书

创建c#脚本

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;public class LoadImageOnClick : MonoBehaviour
{// Start is called before the first frame updatepublic Image imageComponent;public Button button;void Start(){button = GetComponent<Button>();imageComponent = GetComponent<Image>();// 检查按钮是否成功获取if (button != null){button.onClick.AddListener(LoadAndShowImage);}else{Debug.LogError("Button component not found!");}}void LoadAndShowImage(){FileStream fs = new FileStream(@"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg", FileMode.Open, FileAccess.Read);fs.Seek(0, SeekOrigin.Begin);//游标的操作,可有可无byte[] bytes = new byte[fs.Length];//生命字节,用来存储读取到的图片字节try{fs.Read(bytes, 0, bytes.Length);//开始读取,这里最好用trycatch语句,防止读取失败报错}catch (Exception e){Debug.Log(e);}fs.Close();//切记关闭int width = 800;//图片的宽(这里两个参数可以提到方法参数中)int height = 800;//图片的高(这里说个题外话,pico相关的开发,这里不能大于4k×4k不然会显示异常,当时开发pico的时候应为这个问题找了大半天原因,因为美术给的图是6000*3600,导致出现切几张图后就黑屏了。。。Texture2D texture = new Texture2D(width, height);if (texture.LoadImage(bytes)){print("图片加载完毕 ");}else{print("图片尚未加载");}// 加载图片//  Texture2D texture = Resources.Load<Texture2D>("YourImageName"); // 替换 "YourImageName" 为您的图片资源名称// 检查图片是否成功加载if (texture != null){print("显示加载的图片 ");imageComponent.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);print("显示end ");}else{print("Failed to load image ");Debug.LogError("Failed to load image!");}}// Update is called once per framevoid Update(){}
}

自己创建gui组件:

操作步骤:

  1. 创建一个空对象,并将该脚本挂载到该对象上。
  2. 在场景中添加一个Canvas对象,并将Canvas的Render Mode设置为Screen Space - Overlay。
  3. 在Canvas下创建一个Image对象,并将Image组件拖拽到脚本的image字段上。
  4. 将要显示的图片资源拖拽到脚本的sprite字段上。
  5. 运行游戏,图片将会在场景中显示出来。

另一种方法:把脚本拖拽到Canvas下。

在脚本image属性中绑定脚本的变量。

入门教程:

Unity读取项目文件夹图片,PC端_unity 获取配置文件中的图片-CSDN博客

读取图片:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;public class Choose : MonoBehaviour
{private GameObject canvas;private Button _btn;private GameObject button;private List<Texture2D> images = new List<Texture2D>();void Start(){canvas = GameObject.Find("Canvas/Scroll View/Viewport/Content");load();for (int i = 0; i < images.Count; i++){button = new GameObject("Button" + i, typeof(Button), typeof(RectTransform), typeof(Image));  //创建一个GameObject 加入Button组件button.transform.SetParent(this.canvas.transform);  //把Canvas设置成Button的父物体_btn = button.GetComponent<Button>();   //获得Button的Button组件//先创建一个Texture2D对象,用于把流数据转成Texture2DSprite sprite = Sprite.Create(images[i], new Rect(0, 0, images[i].width, images[i].height), Vector2.zero);button.GetComponent<Image>().sprite = sprite;button.GetComponent<Button>().onClick.AddListener(ChooseButton);}}/// <summary>/// 加载文件夹内图片/// </summary>void load(){List<string> filePaths = new List<string>();string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";string[] ImageType = imgtype.Split('|');for (int i = 0; i < ImageType.Length; i++){//获取Application.dataPath文件夹下所有的图片路径  string[] dirs = Directory.GetFiles((Application.dataPath + "/Resources/Screenshot/"), ImageType[i]);for (int j = 0; j < dirs.Length; j++){filePaths.Add(dirs[j]);}}for (int i = 0; i < filePaths.Count; i++){Texture2D tx = new Texture2D(100, 100);tx.LoadImage(getImageByte(filePaths[i]));images.Add(tx);}}/// <summary>  /// 根据图片路径返回图片的字节流byte[]  /// </summary>  /// <param name="imagePath">图片路径</param>  /// <returns>返回的字节流</returns>  private static byte[] getImageByte(string imagePath){FileStream files = new FileStream(imagePath, FileMode.Open);byte[] imgByte = new byte[files.Length];files.Read(imgByte, 0, imgByte.Length);files.Close();return imgByte;}public void ChooseButton(){//UGUI上Button按钮事件Debug.Log("按下了");}}


Unity读取图片并显示到UI中

Unity读取图片并显示到UI中_unity 显示图片-CSDN博客

显示双目相机,可以跑通

GitHub - Sliicy/VR-USB-Camera-Viewer: Unity Project that enables viewing 2 USB cameras in VR as a stereograph image.

unity3d显示图片

Unity3d中(加载(内部、外部))显示图片(sprite、texture2d) - 哔哩哔哩

有2d的,3d的,可以对着视频跑通。

GitHub - creativeIKEP/BlazePoseBarracuda: BlazePoseBarracuda is a human 2D/3D pose estimation neural network that runs the Mediapipe Pose (BlazePose) pipeline on the Unity Barracuda with GPU.

面部的两个项目,需要再看看

GitHub - creativeIKEP/HolisticMotionCapture: HolisticMotionCapture is an application and package that can capture the motion of a person with only a monocular color camera and move the VRM avatar's pose, face, and hands.

GitHub - creativeIKEP/HolisticBarracuda: HolisticBarracuda is the Unity Package that simultaneously estimates 33 pose, 21 per-hand, and 468 facial landmarks on the Unity Barracuda with GPU.

这个好像可以:

GitHub - natmlx/movenet-3d-unity: MoveNet 3D pose detection sample in Unity Engine.


 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Day7.
  • c#通过反射完成对象自动映射
  • Netty——基于Java NIO的网络通信框架
  • 如何运行心理学知识(心流)来指导工作和生活
  • JVM-虚拟机栈
  • 计算机视觉主要知识点
  • abap - 发送邮件,邮件正文带表格和excel附件
  • 使用python-numpy实现一个简单神经网络
  • 《剑指 Offer》专项突破版 - 面试题 38、39 和 40 : 通过三道面试题详解单调栈(C++ 实现)
  • 蓝桥杯Web应用开发-CSS3 新特性
  • 机器学习与深度学习
  • 【笔记】Harmony学习:下载安装 DevEco Studio 开发工具IDE
  • gem5学习(19):gem5内存系统——The gem5 Memory System
  • transformer剪枝论文汇总
  • 使用内联函数,降低函数调用开销,实现移动时绘制
  • 【Leetcode】104. 二叉树的最大深度
  • 【Linux系统编程】快速查找errno错误码信息
  • Angularjs之国际化
  • CentOS7简单部署NFS
  • JavaScript新鲜事·第5期
  • MySQL用户中的%到底包不包括localhost?
  • Odoo domain写法及运用
  • pdf文件如何在线转换为jpg图片
  • 阿里云前端周刊 - 第 26 期
  • 创建一种深思熟虑的文化
  • 从setTimeout-setInterval看JS线程
  • 跨域
  • 为视图添加丝滑的水波纹
  • 新版博客前端前瞻
  • 再次简单明了总结flex布局,一看就懂...
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​2020 年大前端技术趋势解读
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​马来语翻译中文去哪比较好?
  • # 服务治理中间件详解:Spring Cloud与Dubbo
  • (¥1011)-(一千零一拾一元整)输出
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第1节 (全局数据、栈和堆)
  • (SpringBoot)第二章:Spring创建和使用
  • (二)测试工具
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (算法)Travel Information Center
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)ORM
  • (转)VC++中ondraw在什么时候调用的
  • (自用)gtest单元测试
  • **《Linux/Unix系统编程手册》读书笔记24章**
  • .NET Core WebAPI中封装Swagger配置
  • .NET DataGridView数据绑定说明
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .Net 执行Linux下多行shell命令方法
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化