Unity3D: Flipping a Texture

So today, we were trying to horizontally flip a texture to be used within GUI. There is no option to flip a texture via the inspector so we had to do this through our script.

Here’s the little function we used for this, it’s in unityscript but can very easily be converted to C# by just changing how variables are declared. This can also be adjusted to flip things vertically.

function FlipTexture(orig:Texture2D):Texture2D {
var flip:Texture2D = new Texture2D(orig.width,orig.height);
var xN:int = orig.width;
var yN:int = orig.height;

for(var i:int=0; i < xN; i++) {
for(var j:int=0; j < yN; j++) {
flip.SetPixel(xN-i-1, j, orig.GetPixel(i,j));
}
}

flip.Apply();
return flip;
}

11 thoughts on “Unity3D: Flipping a Texture

  1. Javier Ortiz says:

    Needed some pics of the results 😉

    Like

  2. Javier Ortiz says:

    I’ve been coding for a long time in Java and although I still prefer Java overall with blogs like this and some time working with you, I’ve learned to like C just a bit more ;). You have really shown us that girls can really code and that’s why I’m nominating you to the Creative Blogger’s Award! Keep the TCG coming!

    Go here for details: https://dreamgardening.wordpress.com/2015/03/26/the-creative-bloggers-award/

    Like

  3. Sean Loper says:

    I like it, it’s very short and simple, I hope you don’t mind I made a C# version of it and linked to the blog!

    // Converted to C#, originally from https://girlscancode.wordpress.com/2015/03/02/unity3d-flipping-a-texture/
    Texture2D FlipText(Texture2D original)
    {
    // We create a new texture so we don't change the old one!
    Texture2D flip = new Texture2D(original.width,original.height);
    // These for loops are for running through each individual pixel and then replacing them in the new texture.
    for(int i=0; i < flip.width; i++) {
    for(int j=0; j < flip.height; j++) {
    flip.SetPixel(flip.widthi1, j, original.GetPixel(i,j));
    }
    }
    // We apply the changes to our new texture
    flip.Apply();
    // Then we send it on our marry little way!
    return flip;
    }

    view raw
    TextureFlip.cs
    hosted with ❤ by GitHub

    Liked by 1 person

  4. Here’s a one liner that I was able to use to do something similar.

    using System.Linq;
    target,SetPixels32(target.GetPixels32().Reverse().ToArray());

    Liked by 1 person

  5. Nikos says:

    This one is cpu-bound, so if its just displaying, you can just use a material with a shader that flips the texcoords of each vertex to 1-input_tex_coords in the vert shader or if you do it in the frag shader, you could sample using 1-vert_output_coords.
    Certainly a lot faster than doing it via script and while this way uses shaders, its a very simple version of them, so it should be rather easy to get working. And you wouldn’t waste cpu time to do it, especially if its to be done in multiple UI elements.

    Like

    • Nikos says:

      Just did it, simple as adding a material to a UI Image Component, creating a simple Unlit shader through Unity to add all the boilerplate, set the material to use that new shader and in the shader you just do:
      o.uv = TRANSFORM_TEX(v.uv, _MainTex); //This is provided
      o.uv.x = 1 – o.uv.x; //This is what you add
      You can see the effect in the editor without pressing play.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s