Last active
August 14, 2020 15:14
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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