Unity 180 旋转 Texture2D,或者翻转两者

Unity 180 rotation for a Texture2D, or maybe flip both

我需要一点帮助。我正在将 png 加载到 Texture2D 中,并使用我找到的以下脚本设法将其翻转到 y 轴上。我现在需要将它翻转到 x 轴上。我知道应该做一个小的修改,但我还没有得到我想要的结果。

    Texture2D FlipTexture(Texture2D original){
    Texture2D flipped = new Texture2D(original.width,original.height);

    int xN = original.width;
    int yN = original.height;

    for(int i=0;i<xN;i++){
        for(int j=0;j<yN;j++){
            flipped.SetPixel(xN-i-1, j, original.GetPixel(i,j));
        }
    }

    flipped.Apply();

    return flipped;
}

说 "pix" 是 png,

Texture2D photo;
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
// (256 is just an example size)

这完全旋转了 png 180 度

System.Array.Reverse(pix, 0, pix.Length);

这会在垂直轴周围镜像 PNG

        for(int row=0;row<256;++row)
            System.Array.Reverse(pix, row*256, 256);
Texture2D FlipTexture(Texture2D original, bool upSideDown = true)
{

    Texture2D flipped = new Texture2D(original.width, original.height);

    int xN = original.width;
    int yN = original.height;


    for (int i = 0; i < xN; i++)
    {
        for (int j = 0; j < yN; j++)
        {
            if (upSideDown)
            {
                flipped.SetPixel(j, xN - i - 1, original.GetPixel(j, i));
            }
            else
            {
                flipped.SetPixel(xN - i - 1, j, original.GetPixel(i, j));
            }
        }
    }
    flipped.Apply();

    return flipped;
}

调用它:

FlipTexture(camTexture, true); //颠倒

FlipTexture(camTexture, false); //横向

这会颠倒纹理:

int width = texture.width;
int height = texture.height;
Texture2D snap = new Texture2D(width, height);
Color[] pixels = texture.GetPixels();
Color[] pixelsFlipped = new Color[pixels.Length];

for (int i = 0; i < height; i++)
{
    Array.Copy(pixels, i*width, pixelsFlipped, (height-i-1) * width , width);
}

snap.SetPixels(pixelsFlipped);
snap.Apply();