Search Unity

Flipping Texture2D/Image within Unity

Discussion in 'Editor & General Support' started by carnevalle, Dec 2, 2009.

  1. carnevalle

    carnevalle

    Joined:
    Jun 25, 2009
    Posts:
    39
    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.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Change the x/y scale from positive to negative.

    --Eric
     
  3. carnevalle

    carnevalle

    Joined:
    Jun 25, 2009
    Posts:
    39
    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?
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You just need to set the scale value (in the inspector panel) for the object that has the texture.
     
    Yspadin and tabworldmedia like this.
  5. carnevalle

    carnevalle

    Joined:
    Jun 25, 2009
    Posts:
    39
    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:
    Code (csharp):
    1.  
    2. Texture2D background;
    3. Texture2D character;
    4.  
    5. GUI.DrawTexture(new Rect(0,0, background.width, background.height), background);
    6. GUI.DrawTexture(new Rect(0,0, character.width, character.height), character);
    7.  
    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.
     

    Attached Files:

  6. carnevalle

    carnevalle

    Joined:
    Jun 25, 2009
    Posts:
    39
    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?
     
  7. bradjensen68

    bradjensen68

    Joined:
    Feb 2, 2009
    Posts:
    274
    Yeah, just create an empty game object and then attach your script to that. You don't need the GUI texture object.
     
    Panner_ likes this.
  8. RodrigoSeVeN

    RodrigoSeVeN

    Joined:
    Jul 10, 2010
    Posts:
    15
    hey carnevalle,

    you can draw a flipped texture simply by typing:

    Code (csharp):
    1. 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:

    Code (csharp):
    1. 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~
     
    bosung90 likes this.
  9. Xroft666

    Xroft666

    Joined:
    Dec 24, 2010
    Posts:
    24
    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?
     
  10. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    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.
     
  11. Xroft666

    Xroft666

    Joined:
    Dec 24, 2010
    Posts:
    24
    Thanks! Exactly what I need.
     
  12. SuddenCrusher

    SuddenCrusher

    Joined:
    Nov 17, 2013
    Posts:
    1
    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
     
  13. chanfort

    chanfort

    Joined:
    Dec 15, 2013
    Posts:
    641
    You can try to flip from C# by calling this function:
    Code (CSharp):
    1.  
    2.         Texture2D FlipTexture(Texture2D original){
    3.             Texture2D flipped = new Texture2D(original.width,original.height);
    4.          
    5.             int xN = original.width;
    6.             int yN = original.height;
    7.          
    8.          
    9.            for(int i=0;i<xN;i++){
    10.               for(int j=0;j<yN;j++){
    11.                   flipped.SetPixel(xN-i-1, j, original.GetPixel(i,j));
    12.               }
    13.            }
    14.             flipped.Apply();
    15.          
    16.             return flipped;
    17.         }
    18.    
    But your texture should have "Read/Write Enabled" flag selected before going with this.
     
  14. MrMylad

    MrMylad

    Joined:
    Jan 6, 2022
    Posts:
    6
    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)
     

    Attached Files:

    nekowei and Panner_ like this.
  15. Stepan_87

    Stepan_87

    Joined:
    May 20, 2019
    Posts:
    1