• 【100个 Unity实用技能】| Unity将本地图片文件显示到Image组件中 通用方法整理


    请添加图片描述

    Unity 小科普

    老规矩,先介绍一下 Unity 的科普小知识:

    • Unity是 实时3D互动内容创作和运营平台 。
    • 包括游戏开发美术建筑汽车设计影视在内的所有创作者,借助 Unity 将创意变成现实。
    • Unity 平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机平板电脑PC游戏主机增强现实虚拟现实设备。
    • 也可以简单把 Unity 理解为一个游戏引擎,可以用来专业制作游戏
    • 🎬 博客主页:https://xiaoy.blog.csdn.net

    • 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉

    • 🎄 学习专栏推荐:Unity系统学习专栏

    • 🌲 游戏制作专栏推荐:游戏制作

    • 🌲Unity实战100例专栏推荐:Unity 实战100例 教程

    • 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

    • 📆 未来很长,值得我们全力奔赴更美好的生活✨

    • ------------------❤️分割线❤️-------------------------

    请添加图片描述


    Unity 实用小技能学习

    Unity将本地图片文件显示到Image组件中 通用方法整理

    本文总结了两种将本地图片文件显示到Image组件中 的两种方法,下面一起来看一下吧!

    方法一:通过命名空间 System.IO 加载本地图片文件

    using System.IO;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Demo : MonoBehaviour
    {
    	public Image _faceSearchImage;
    	private Texture2D m_Tex;
    
    	/// 
    	/// 根据路径读取本地文件并转换为Texture2D文件
    	/// 
    	/// 
    	private void LoadFromFile(string path)
    	{
    		m_Tex = new Texture2D(1, 1);
    		//读取图片字节流
    		m_Tex.LoadImage(ReadPNG(path));
    
    		//变换格式
    		Sprite tempSprite = Sprite.Create(m_Tex, new Rect(0, 0, m_Tex.width, m_Tex.height), new Vector2(10, 10));
    		_faceSearchImage.sprite = tempSprite;//赋值
    	}
    
    	private byte[] ReadPNG(string path)
    	{
    		Debug.Log(path);
    		FileStream fileStream = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read);
    
    		fileStream.Seek(0, SeekOrigin.Begin);
    		//创建文件长度的buffer
    		byte[] binary = new byte[fileStream.Length];
    		fileStream.Read(binary, 0, (int)fileStream.Length);
    		fileStream.Close();
    		fileStream.Dispose();
    		fileStream = null;
    
    		return binary;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    方法二:通使用 UnityWebRequest 加载本地图片文件

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;
    
    public class Demo : MonoBehaviour
    {
    	public Image _faceSearchImage;
    
    	//根据路径或者URL读取本地文件并转换为Texture2D文件
    	public void GetBookSprite(string url)
    	{
    		StartCoroutine(DownSprite(url));
    	}
    
    	IEnumerator DownSprite(string url)
    	{
    		var uri = new System.Uri(Path.Combine(url));
    		UnityWebRequest www = UnityWebRequest.Get(uri);
    		DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
    		www.downloadHandler = texDl;
    
    		yield return www.SendWebRequest();
    
    		if (www.isHttpError || www.isNetworkError)
    		{
    			Debug.LogError(www.error);
    		}
    		else
    		{
    			Texture2D tex = new Texture2D(1, 1);
    			tex = texDl.texture;
    			Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
    			_faceSearchImage.sprite = sprite;
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在这里插入图片描述


    在这里插入图片描述

  • 相关阅读:
    6.26CF模拟赛E:价格最大化题解
    【matplotlib基础】--几何图形
    08、SpringCloud之SpringBoot-Admin监控组件学习笔记
    【多线程】Synchronized 用法详解
    关于地图GIS开发事项的一次实践整理(上)
    分布式应用之监控平台zabbix的认识与搭建
    Let 与 Const 区别 (2022.11.7)
    vmtouch——Linux下的文件缓存管理神器
    06 | 链表(上):如何实现LRU缓存淘汰算法?
    新能源汽车行业出口ERP管理解决方案
  • 原文地址:https://blog.csdn.net/zhangay1998/article/details/127466336