Search Unity

Rotate A GUI SPrite/element/texture

Discussion in 'Immediate Mode GUI (IMGUI)' started by grimmy, Dec 14, 2009.

  1. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    409
    I want to perfom one of the most simple tasks possible -rotating a sprite ...but the forums just show up a pethora of way overcomplicated methods which I have tried and dont seem even to work. I have heard about GUIUtility.RotateAround pivot but that doesnt seem to work. Does anyone know a simple one liner that I can put in the Update loop or OnGUI loop to rotate a sprite..

    Ps..when I say 'sprite' I mean a game object with a GuiTexture attached. Maybe this is the wrong kind of object to use. Can someone clarify....with clarity?

    Ta
     
  2. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    Haven't tried it, but if you have an empty game object with the GUITexture parented under it, you could just rotate the game object...
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It works fine actually.

    Yes, since GUITextures can't be rotated, only stuff in OnGUI. Of course, you could always just use a texture on a 2-polygon plane in 3D space, then you can rotate them any which way you like. (This is what I normally use for sprites.)

    --Eric
     
  4. ooppari

    ooppari

    Joined:
    Jan 29, 2009
    Posts:
    68
    If you draw texture inside GUI.Window and rotate that window ( GUIUtility.RotateAroundPivot ) -> Texture also rotates. Just remember to rotate everything back after drawing that window if that's not last window drawn during that frame.
     
  5. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    373
    Actually it's a lot simpler:

    Code (csharp):
    1.  
    2. Matrix4x4 matrixBackup = GUI.matrix;
    3. {
    4.     GUIUtility.RotateAroundPivot(angle, pos);
    5.     GUI.DrawTexture(rect, texture);    
    6. }
    7. GUI.matrix = matrixBackup;
    8.  
    'pos' is the the screen position of the center of the sprite in case you want to rotate around the center

    Note that you can additionally use ScaleAroundPivot if you also need to scale your image.
     
  6. himan

    himan

    Joined:
    Jan 27, 2010
    Posts:
    16
    hi. i can not use this code.Please explain more :oops:
     
  7. lonewolfaka9

    lonewolfaka9

    Joined:
    Dec 3, 2009
    Posts:
    6
    dint work in my case throwing errors


     
  8. tonyoakden

    tonyoakden

    Joined:
    Nov 13, 2009
    Posts:
    135
    I don't think Bernard intended the code to be taken literally. Here is a version in JS which rotates a texture in the middle of the screen. You need to have:

    Code (csharp):
    1. public var testTexture: Texture2D = null;
    At the top of the file and then you need to assign a texture to testTexture using the editor inspector.

    Then this code in OnGUI() somewhere:

    Code (csharp):
    1.  
    2. var matrixBackup:Matrix4x4  = GUI.matrix;
    3. var thisAngle:float = Time.frameCount * 2;
    4. var pos:Vector2 = Vector2(425,425);
    5. GUIUtility.RotateAroundPivot(thisAngle, pos);
    6. var thisRect:Rect = Rect(400,400,50,50);
    7. GUI.DrawTexture(thisRect, testTexture);      
    8. GUI.matrix = matrixBackup;
    9.  
    When run the code will rotate the texture in approximately the middle of the screen using the frame count to control rotation. You should be able to modify the code to rotate whatever you want, wherever you want it.

    I tested this and it works on in my project.
     
  9. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Forgive a complete noob here. Have tried to attach the code to an empty game object, GUI texture, cube primitive, the camera, and run out of ideas now where this is to attach to and what to try next to get it to work. Where do i attach this code to get it to work?
     
  10. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    To answer myself, it is a bad idea to test a GUI element that sits at 400x400 when you test it in a 200x200 window. No wonder i didn`t see a result ... :D

    Got it working. The script is attached to an empty Gameobject now :)

    Just for the sake of providing a working example that you can simply open and have a look at, here the project file. Credits goes of course to tonyoakden here. Thanks for explanation :)
     
  11. EricJ13

    EricJ13

    Joined:
    Feb 28, 2009
    Posts:
    355
    tonyoakden, you dah man. Thanx A LOT. I always figured since GUITexture had a transform that I could rotate GUI elements that way, and I was super bummed out when I found out I couldn't. I don't think I'd have been able to figure out a way to get the gauges in my game working like I want, the method you describe has saved the day. And thanx too Tiles, having an example to look at brought it all together.

    EDIT: Cripes, gotta give credit where it's due... thanx as well to bernardfrancois!
     
  12. forest_linshs

    forest_linshs

    Joined:
    Jan 6, 2010
    Posts:
    39
    Solved my problem, thank you all!!!
     
  13. AlexMoore

    AlexMoore

    Joined:
    Dec 20, 2012
    Posts:
    1
    I know it's 3 years later, but this solved my problem too - thank you tonyoakden!
     
  14. danvlad

    danvlad

    Joined:
    Jan 19, 2010
    Posts:
    8
    Thank's tonyoakden for your explanation !
    :cool:

     
    Last edited: Oct 21, 2013