Skip to content

Instantly share code, notes, and snippets.

@adamski11
Last active August 14, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamski11/360d593a3b44653e8e59ca47f7589841 to your computer and use it in GitHub Desktop.
Save adamski11/360d593a3b44653e8e59ca47f7589841 to your computer and use it in GitHub Desktop.
I have found no solution for flipping a texture in Unity that is particularly efficient without going into multi-threading, which may or may not be possible depending on the project at hand, as to truly flip the texture you have to move all the pixels in the image around to "flip" the image. I've attempted to create the most efficient method for…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextureFlip
{
public static Texture2D FlipTexture(this Texture2D original)
{
int textureWidth = original.width;
int textureHeight = original.height;
Color[] colorArray = original.GetPixels();
for (int j = 0; j < textureHeight; j++)
{
int rowStart = 0;
int rowEnd = textureWidth - 1;
while (rowStart < rowEnd)
{
Color hold = colorArray[(j * textureWidth) + (rowStart)];
colorArray[(j * textureWidth) + (rowStart)] = colorArray[(j * textureWidth) + (rowEnd)];
colorArray[(j * textureWidth) + (rowEnd)] = hold;
rowStart++;
rowEnd--;
}
}
Texture2D finalFlippedTexture = new Texture2D(original.width, original.height);
finalFlippedTexture.SetPixels(colorArray);
finalFlippedTexture.Apply();
return finalFlippedTexture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment