Flip a Texture?

Hi there! Allow me to ask you another newbie question please…
In photoshop I flip a 2d image like this: edit → transform → flip; how would I do that in Unity?
I mean WITH code.

See, I did some web researches but every click draw me even more far away, toward topics related to textures but not to the solution.
I know every Shader has this Tiling and Offset thing, and I’ve read that to flip a texture horizontally you just need to change to -1 the Tiling in your inspector… fact is that I couldn’t be able to grasp how to do that with code.

I tried a brutish renderer.lightmapTilingOffset = new Vector4(-1,1,0,0) but nothing happens, I also tried renderer.material.mainTextureTiling = new Vector2(-1,0) but ofcourse mainTextureTiling doesn’t exist ( why?).

I’m ignorant but eager to know how, I’m not asking for plain code, maybe just a push in the right direction… my searches brought me here: http://docs.unity3d.com/Documentation/Manual/ShaderTut1.html; This is pertinent but non exactly what I need here, am I wrong? I also stumbled upon this Unity - Scripting API: Material but as you can see, there is mainTextureOffset but not Tiling.

I’m missing something… thanks a lot for your patience ( AND if it turns out an answer already exist, forgive my thoughtlessness)!

renderer.material.SetTextureScale(“_MainTex”, new Vector2(-1,1));

Note this assumes that your shaders main texture is defined as _MainTex. This is true of most shaders but not all. While not specific to your question, changing material properties takes a shared material and generates a new material instance and therefore breaks draw call batching. An alternate method of flipping the texture that does not break batching is to change the UV coordinates directly in the mesh (more complicated).

Another method would be to set the scale of the SpriteRenderer object directly:

Vector3 scale = renderer.transform.localScale;
scale.x = -scale.x;
renderer.transform.localScale = scale;

Easiest by far and will get batched since Unity 4.5.X. Or the same thing via rotation (see the comment below that the scale might be problematic in some cases):

Vector3 angles = renderer.transform.localEulerAngles;
angles.y = 180;
renderer.transform.localEulerAngles = angles;

Of course, this will depend on the pivot of the Sprite. If set to center, it will behave as expected. Otherwise you’ll need to move it a bit.

Sadly no solution is going to be really efficient without going into threading, which you may or may not be able to do, as to flip the texture you have to move all the pixels in the image around to truly “flip” the image. I’ve attempted to create the most efficient method for flipping a texture horizontally. This avoids performing multiple get or set pixels, and processes the pixels 2 at a time. This should work for your project:

 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;
    }

As this has already been answered. Yet posting another solution for anyone who is looking for a better solution with efficiency with performance. I would suggest creating a compute shader. As they will run on GPU as parallel threads, they can give an extreme performance in this kind of scenario.


Here a quick google search took me to this Christian Mills - How to Flip an Image With a Compute Shader