Unity中 Image常用的转换函数

本文介绍了如何在Unity中进行各种纹理转换操作,包括字节到Texture2D、屏幕截图、Texture2D到JPG/PNG、RenderTexture到Texture2D/Sprite及格式文件等。提供了丰富的代码示例,帮助开发者掌握纹理处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将字节转换成Texture2D、屏幕截图、Texture2D转换成JPG格式数据、Texture2D转换成JPG格式数据等转换。
1.将字节转换成Texture2D
    /// <summary>
	/// 将字节转换成Texture2D
	/// </summary>
	/// <param name="data"></param>
	/// <returns>Texture2D</returns>
	public static Texture2D ByteToTexture(byte[] data)
	{
		Texture2D texture2D = null;
		if (data != null)
		{
			texture2D = new Texture2D(0, 0);
			texture2D.LoadImage(data);
		}
		return texture2D;
	}
2.屏幕截图
    /// <summary>
	/// 屏幕的截图
	/// </summary>
	public static void GetTexture2DFromScreen(Rect rect, UnityAction<Texture2D> unityAction)
	{
		StartCoroutine(GetTexture2DFromScreen(unityAction, rect));

	}
	private static IEnumerator GetTexture2DFromScreen(UnityAction<Texture2D> unityAction, Rect rect)
	{
		yield return new WaitForEndOfFrame();
		Texture2D texture2D = new Texture2D((int)rect.width, (int)rect.height);
		texture2D.ReadPixels(rect, 0, 0);
		texture2D.Apply();
		unityAction?.Invoke(texture2D);
	}
3.将Texture2D转换成JPG格式数据

下面展示一些 内联代码片

	/// <summary>
	/// 将Texture2D转换成JPG格式数据
	/// </summary>
	/// <param name="texture2D"></param>
	/// <returns>JPG编码的字节数据</returns>
	public static byte[] Texture2DToJPG(Texture2D texture2D)
	{
		if (texture2D != null)
		{
			return texture2D.EncodeToJPG();
		}
		return null;
	}
4.将Texture2D转换成JPG格式数据,并且可以压缩
    /// <summary>
	/// 将Texture2D转换成JPG格式数据,并且可以压缩
	/// </summary>
	/// <param name="texture2D">目标texture2D</param>
	/// <param name="quality">压缩质量</param>
	/// <returns></returns>
	public static byte[] Texture2DToJPG(Texture2D texture2D, int quality)
	{
		if (texture2D != null)
		{
			return texture2D.EncodeToJPG(quality);
		}
		return null;
	}
5.将Texture2D转换成PNG格式数据
    /// <summary>
	/// 将Texture2D转换成PNG格式数据
	/// </summary>
	/// <param name="texture2D"></param>
	/// <returns></returns>

	public static byte[] Texture2DToPNG(Texture2D texture2D)
	{
		if (texture2D != null)
		{
			return texture2D.EncodeToPNG();
		}
		return null;
	}
6.Texture2D转换成Sprite
    /// <summary>
	/// 将Texture2D转换成PNG格式数据
	/// </summary>
	/// <param name="texture2D"></param>
	/// <returns></returns>

	public static byte[] Texture2DToPNG(Texture2D texture2D)
	{
		if (texture2D != null)
		{
			return texture2D.EncodeToPNG();
		}
		return null;
	}
7.将RenderTexture转换成Texture2D
    /// <summary>
	/// 将RenderTexture转换成Texture2D
	/// </summary>
	/// <param name="renderTexture"></param>
	/// <returns>Texture2D</returns>
	public static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture)
	{

		if (renderTexture != null)
		{
			Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
			RenderTexture previous = RenderTexture.active;
			RenderTexture.active = renderTexture;
			texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
			RenderTexture.active = previous;
			texture2D.Apply();
			return texture2D;
		}
		return null;
	}
8.将RenderTexture2D转换成Sprite
  /// <summary>
	/// 将RenderTexture2D转换成Sprite
	/// </summary>
	/// <param name="renderTexture"></param>
	/// <returns>Sprite</returns>
	public static Sprite RenderTextureToSprite(RenderTexture renderTexture)
	{
		if (renderTexture != null)
		{
			Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
			RenderTexture previous = RenderTexture.active;
			RenderTexture.active = renderTexture;
			texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
			RenderTexture.active = previous;
			texture2D.Apply();
			return Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.one * 0.5f);
		}
		return null;
	}
9.将RenderTexture转换成PNG格式的字节数组
 /// <summary>
	/// 将RenderTexture转换成PNG格式的字节数组
	/// </summary>
	/// <param name="renderTexture"></param>
	/// <returns>PNG格式的字节数组</returns>
	public static byte[] RenderTextureToPNG(RenderTexture renderTexture)
	{
		if (renderTexture != null)
		{
			Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
			RenderTexture previous = RenderTexture.active;
			RenderTexture.active = renderTexture;
			texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
			RenderTexture.active = previous;
			texture2D.Apply();
			return texture2D.EncodeToPNG();
		}
		return null;
	}

10.将RenderTexture转换成JPG格式字节数组
/// <summary>
	/// 将RenderTexture转换成JPG格式字节数组
	/// </summary>
	/// <param name="renderTexture"></param>
	/// <returns>JPG格式字节数组</returns>
	public static byte[] RenderTextureToJPG(RenderTexture renderTexture)
	{
		if (renderTexture != null)
		{
			Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
			RenderTexture previous = RenderTexture.active;
			RenderTexture.active = renderTexture;
			texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
			RenderTexture.active = previous;
			texture2D.Apply();
			return texture2D.EncodeToJPG();
		}
		return null;
	}
11.将Rendertexture转换成JPG格式的自己数组并且可以选择压缩质量
/// <summary>
	/// 将Rendertexture转换成JPG格式的自己数组并且可以选择压缩质量
	/// </summary>
	/// <param name="renderTexture"></param>
	/// <param name="quality">压缩质量</param>
	/// <returns>JPG格式的自己数组</returns>
	public static byte[] RenderTextureToJPG(RenderTexture renderTexture, int quality)
	{
		if (renderTexture != null)
		{
			Texture2D texture2D = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
			RenderTexture previous = RenderTexture.active;
			RenderTexture.active = renderTexture;
			texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
			RenderTexture.active = previous;
			texture2D.Apply();
			return texture2D.EncodeToJPG(quality);
		}
		return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值