将字节转换成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;
}