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

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 update
    public 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 frame
    void 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对象,用于把流数据转成Texture2D
            Sprite 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.


 


http://www.kler.cn/news/234478.html

相关文章:

  • 中科大计网学习记录笔记(八):FTP | EMail
  • linux进程(进程状态)
  • 再说开源软件
  • 瑞吉外卖实操笔记五----店铺营业状态设置与用户端微信登录实现
  • Junit常用注解
  • 如何在苹果Mac上进行分屏,多任务处理?
  • 深入学习《大学计算机》系列之第1章 1.7节——图灵机的一个例子
  • 蓝桥杯每日一题之内存问题
  • Elementplus报错 [ElOnlyChild] no valid child node found
  • Spring Boot与Kafka集成教程
  • Django模板(二)
  • 每天上班都疲惫不堪,怎么办?
  • 【C/C++ 17】继承
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之ScrollBar组件
  • 安全SCDN有什么作用
  • PMP考试之20240211
  • JAVA中的单例模式->懒汉式
  • 在 Docker 中启动 ROS2 里的 rivz2 和 rqt 出现错误的解决方法
  • 《Django+React前后端分离项目开发实战:爱计划》 01 项目整体概述
  • DP读书:《openEuler操作系统》(九)从IPC到网卡到卡驱动程序
  • 【FPGA开发】Modelsim和Vivado的使用
  • mysql底层结构
  • [C# WPF] DataGrid选中行或选中单元格的背景和字体颜色修改
  • 简单介绍算法的基本概念
  • LeetCode Python -8.字符串转整数
  • golang 集成sentry:PostgreSQL
  • 4核8g服务器能支持多少人访问?- 腾讯云
  • 【Rust】——猜数游戏
  • python打印等边三角形
  • 【0257】关于pg内核shared cache invalidation messages (概念篇)