Unity 图片的旋转
/// <summary>
/// 图片逆时针旋转90度
/// </summary>
/// <param name="src">原图片二进制数据</param>
/// <param name="srcW">原图片宽度</param>
/// <param name="srcH">原图片高度</param>
/// <param name="desTexture">输出目标图片</param>
public static void RotationLeft90(Color32[] src, int srcW, int srcH, Texture2D desTexture)
{
if (pixelsLen != src.Length || des == null)
{
pixelsLen = src.Length;
des = new Color32[pixelsLen];
}
if (desTexture.width != srcH || desTexture.height != srcW)
{
desTexture.Resize(srcH, srcW);
}
for (int i = 0; i < srcW; i++)
{
for (int j = 0; j < srcH; j++)
{
des[i * srcH + j] = src[(srcH - 1 - j) * srcW + i];
}
}
desTexture.SetPixels32(des);
desTexture.Apply();
}
/// <summary>
/// 图片顺时针旋转90度
/// </summary>
/// <param name="src">原图片二进制数据</param>
/// <param name="srcW">原图片宽度</param>
/// <param name="srcH">原图片高度</param>
/// <param name="desTexture">输出目标图片</param>
public static void RotationRight90(Color32[] src, int srcW, int srcH, Texture2D desTexture)
{
if (pixelsLen != src.Length || des == null)
{
pixelsLen = src.Length;
des = new Color32[pixelsLen];
}
if (desTexture.width != srcH || desTexture.height != srcW)
{
desTexture.Resize(srcH, srcW);
}
for (int i = 0; i < srcH; i++)
{
for (int j = 0; j < srcW; j++)
{
des[(srcW - j - 1) * srcH + i] = src[i * srcW + j];
}
}
desTexture.SetPixels32(des);
desTexture.Apply();
}
/// <summary>
/// 图片旋转180度
/// </summary>
/// <param name="src">原图片二进制数据</param>
/// <param name="srcW">原图片宽度</param>
/// <param name="srcH">原图片高度</param>
/// <param name="desTexture">输出目标图片</param>
public static void Rotation180(Color32[] src, int srcW, int srcH, Texture2D desTexture)
{
if (pixelsLen != src.Length || des == null)
{
pixelsLen = src.Length;
des = new Color32[pixelsLen];
}
if (desTexture.width != srcW || desTexture.height != srcH)
{
desTexture.Resize(srcW, srcH);
}
for (int i = 0; i < srcH; i++)
{
for (int j = 0; j < srcW; j++)
{
des[i * srcW + j] = src[(srcH - i) * srcW - j - 1];
}
}
desTexture.SetPixels32(des);
desTexture.Apply();
}
/// <summary>
/// 图片镜像
/// </summary>
/// <param name="src">原图片二进制数据</param>
/// <param name="srcW">原图片宽度</param>
/// <param name="srcH">原图片高度</param>
/// <param name="desTexture">输出目标图片</param>
public static void Mirror(Color32[] src, int srcW, int srcH, Texture2D desTexture)
{
if (pixelsLen != src.Length || des == null)
{
pixelsLen = src.Length;
des = new Color32[pixelsLen];
}
if (desTexture.width != srcW || desTexture.height != srcH)
{
desTexture.Resize(srcW, srcH);
}
for (int i = 0; i < srcH; i++)
{
for (int j = 0; j < srcW; j++)
{
des[i * srcW + j] = src[(i + 1) * srcW - j - 1];
}
}
desTexture.SetPixels32(des);
desTexture.Apply();
}
/// <summary>
/// 图片拷贝
/// </summary>
/// <param name="src">原图片二进制数据</param>
/// <param name="srcW">原图片宽度</param>
/// <param name="srcH">原图片高度</param>
/// <param name="desTexture">输出目标图片</param>
public static void Copy(Color32[] src, int srcW, int srcH, Texture2D desTexture)
{
if (desTexture.width != srcW || desTexture.height != srcH)
{
desTexture.Resize(srcW, srcH);
}
desTexture.SetPixels32(src);
desTexture.Apply();
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步