Flipping Texture2D/Image within Unity

Hey all!

I am having a problem with how to flip a texture in Unity. I am building an ingame interface in which, I want to position a piece of graphic in each corner of the screen.

It is essentially the same piece of graphic, so instead of creating a graphic for topleft and topright corner, I would just like to flip it horizontally. Would there be any easy way to accomplish this?

I hope anyone can help.

1 Like

Change the x/y scale from positive to negative.

–Eric

12 Likes

Hi Eric. Thanks for your reply.

I can’t really seem to find out how to change the x/y scale on a Texture2D. Could you help me with a small code example or a link?

You just need to set the scale value (in the inspector panel) for the object that has the texture.

2 Likes

I think I am probably not describing my problem in a precise manner. I don’t really have an object with a texture that I can manipulate in the Inspector.

I am trying to build i visual interface for my game, and I am trying to do this with the GUI.DrawTexture function.

Here is a code example:

Texture2D background;
Texture2D character;

GUI.DrawTexture(new Rect(0,0, background.width, background.height), background);
GUI.DrawTexture(new Rect(0,0, character.width, character.height), character);

Now, when I want to draw the interface for player #2, which is in the top right corner, I would like to be able to mirror the images of my characters, so I don’t have to do produce a double set of images.

Is it possible at all?

I have tried to attach an image of the top left corner.

233580--8361--$picture_2_946.png

Or am I attacking this problem in a totally wrong manner? Should I instead build the HUD with other elements than using the GUI-framework?

Yeah, just create an empty game object and then attach your script to that. You don’t need the GUI texture object.

1 Like

hey carnevalle,

you can draw a flipped texture simply by typing:

GUI.DrawTexture(new Rect(0,0, -character.width, character.height), character);

in this case, the texture will draw from zero to minus, therefore you just have to change the origin like this:

GUI.DrawTexture(new Rect(Screen.width,0, -character.width, character.height), character);

but what i was really trying to find out how, is to flip a texture in order to draw it flipped as a button(not inside a button).

cheers~

1 Like

Hi everyone! What if my object is not a GUI and I cant change localScale, 'cause it breaks my batching.
I could make another image but maybe there’s simplier way?

If it’s a quad and not a complex shape, you can flip the UVs. i.e. the UVs on the left side and the UVs on the right side of the quad are reversed.

Thanks! Exactly what I need.

wow thanks!! he might of not had the same problem i did i simply put x scale negative and my guy is now facing the other direction simply trying too make my character face both ways when he moves left he faces left he moves right he flips and faces right

You can try to flip from C# by calling this function:

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

But your texture should have “Read/Write Enabled” flag selected before going with this.

9 Likes

Sorry I’m late, a MUCH simpler way to go about things would be to change the x & y ROTATIONS in the inspector

y for horizontal flips and x for vertical

(change it from 0 to 180)

(screenshot)

2 Likes