一段旋转位图的代码,分享一下旋转的方法,其实就是cos,sin的应用,比较简单!
unity3d中C#脚本可用:
Texture2D RotateTexture (Texture2D texture, float eulerAngles)
{
int x;
int y;
int i;
int j;
float phi = eulerAngles / (180 / Mathf.PI);
float sn = Mathf.Sin (phi);
float cs = Mathf.Cos (phi);
Color32[] arr = texture.GetPixels32 ();
Color32[] arr2 = new Color32[arr.Length];
int W = texture.width;
int H = texture.height;
int xc = W / 2;
int yc = H / 2;
for (j=0; j<H; j++) {
for (i=0; i<W; i++) {
arr2 [j * W + i] = new Color32 (0, 0, 0, 0);
x = (int)(cs * (i - xc) + sn * (j - yc) + xc);
y = (int)(-sn * (i - xc) + cs * (j - yc) + yc);
if ((x > -1) && (x < W) && (y > -1) && (y < H)) {
arr2 [j * W + i] = arr [y * W + x];
}
}
}
Texture2D newImg = new Texture2D (W, H);
newImg.SetPixels32 (arr2);
newImg.Apply ();
return newImg;
}