Unity2D的Unity 180旋转,或者可以翻转两者

时间:2016-03-11 21:56:20

标签: c# unity3d textures

我可以使用一点帮助。我正在将一个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;
}

3 个答案:

答案 0 :(得分:6)

说&#34; pix&#34;是一个png,

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

这完全旋转180度

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

这反映了围绕直立轴的PNG

        for(int row=0;row<256;++row)
            System.Array.Reverse(pix, row*256, 256);

答案 1 :(得分:2)

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); // Sideways

答案 2 :(得分:0)

这使纹理上下颠倒:

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();