Unity中 Image常用的转换函数

将字节转换成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;
	}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity通过点击RawImage来获取RenderTexture的映射物体需要编写一些脚本逻辑。首先,在场景创建一个RawImage和一个空物体,并将RawImage设为UI元素。接下来,我们需要在脚本实现点击RawImage的功能。 首先,在需要实现点击事件的脚本添加以下代码段: ```csharp using UnityEngine; using UnityEngine.EventSystems; public class ClickRawImage : MonoBehaviour, IPointerClickHandler { public GameObject mappedObject; // 映射的物体 public RenderTexture renderTexture; // 渲染纹理 public void OnPointerClick(PointerEventData eventData) { // 获取点击的位置 Vector2 localCursor; RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(), eventData.position, null, out localCursor); // 将点击的位置转换为纹理坐标 Vector2 textureCoordinates = new Vector2(localCursor.x / GetComponent<RectTransform>().rect.width + 0.5f, localCursor.y / GetComponent<RectTransform>().rect.height + 0.5f); // 根据纹理坐标获取映射的位置 Ray ray = Camera.main.ViewportPointToRay(textureCoordinates); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // 获取点击位置的映射物体 mappedObject = hit.transform.gameObject; } } } ``` 在脚本,我们需要实现接口`IPointerClickHandler`来响应RawImage的点击事件。在点击事件回调函数`OnPointerClick`,首先计算出点击的位置`localCursor`,然后将其转换为纹理坐标`textureCoordinates`。接着,根据纹理坐标通过Raycast来获取点击位置的映射物体,并将其赋值给`mappedObject`。 最后,将该脚本挂载到RawImage上,并将相应的RenderTexture赋值给脚本的`renderTexture`变量。这样,当点击RawImage时,就可以通过点击位置获取到映射的物体了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值