Skip to content

Instantly share code, notes, and snippets.

@pofulu
Created October 15, 2021 08:55
Show Gist options
  • Save pofulu/c87b5a100de5a1dcbad12c29397db796 to your computer and use it in GitHub Desktop.
Save pofulu/c87b5a100de5a1dcbad12c29397db796 to your computer and use it in GitHub Desktop.
旋轉圖片
using UnityEngine;
public static class TextureTool
{
public static Texture2D rotateTexture(Texture2D originalTexture, bool clockwise)
{
Color32[] original = originalTexture.GetPixels32();
Color32[] rotated = new Color32[original.Length];
int w = originalTexture.width;
int h = originalTexture.height;
int iRotated, iOriginal;
for (int j = 0; j < h; ++j)
{
for (int i = 0; i < w; ++i)
{
iRotated = (i + 1) * h - j - 1;
iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
rotated[iRotated] = original[iOriginal];
}
}
Texture2D rotatedTexture = new Texture2D(h, w);
rotatedTexture.SetPixels32(rotated);
rotatedTexture.Apply();
return rotatedTexture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment