Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animation of GUI Buttons

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jaapstudent, Jun 10, 2009.

  1. Jaapstudent

    Jaapstudent

    Joined:
    Mar 27, 2009
    Posts:
    24
    Hello,

    i was wondering if it was possible to animated the GUI Button it self.

    now they are static, but it will be cool to have a animation in the buttons.

    So if i was clicking on one button, a different menu will appear. but not directly but with an motion-tween.

    Like you can do in flash.


    I have create a NEW GUIStyle, with some transparent background.

    but to scale of moving around this GUI-button, i can't find the way to do this.

    I'm using Unity Indy!

    i hope some of the experts know if it's possible?!

    if it's, what is the way to do it?

    thanks
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Animating your GUI elements is trivial. Instead of hard-coding a rect value just store that in a variable (or variables) whose value(s) you animate. Bare bones example:

    Code (csharp):
    1. var ButtonLeft = 0.0;
    2.  
    3. function Update () {
    4.   ButtonLeft += (Time.deltaTime * 10.0);
    5. }
    6.  
    7. function OnGUI () {
    8.   var tButtonRect = new Rect(ButtonLeft, 20.0, 50.0, 20.0);
    9.   if (GUI.Button(tButtonRect, "Moving Button!")) {
    10.     Debug.Log("You clicked the moving button...");
    11.   }
    12. }
    Again, the above is a very basic example and the button will eventually disappear off-screen, but it should prove the point. Store the relevant position/size/rect values in variables, then use an Update function or a coroutine to animate those values as needed, making your GUI elements move about as needed. :)
     
  3. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    Hey Tom,

    Could you tell me if it is possible to animate the alpha of a texture in the GUI system in the same manner?

    All I am looking to do is animate the alpha so my logo screens fade off and on. The textures don't have an alpha channel, but I can easily add one if it will give me the effect I'm after.

    The logo screens are all stored in separate scenes, and I am using a GUI.Box that is skinned to the appropriate textures using multiple custom GUIStyle's in a custom GUISkin.
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Yup! Please look at the above as if it's a template example and build from there for any variable value used in your GUI.

    Code (csharp):
    1. var MyGUIAlpha = 0.0;
    2.  
    3. function Update () {
    4.  
    5.   if (MyGUIAlpha < 1.0) {
    6.     MyGUIAlpha += 0.1;
    7.   }
    8.  
    9. }
    10.  
    11. function OnGUI () {
    12.  
    13.   GUI.color = new Color(1.0, 1.0, 1.0, MyGUIAlpha);
    14.   // Draw GUI stuff here
    15.  
    16. }
    The above is a fairly dumb/simple example but again, it proves the point (I hope). Store whatever value you need in a variable then animate it however you want. Make FadeIn/FadeOut functions, etc. :)
     
  5. sn4k3

    sn4k3

    Joined:
    May 16, 2009
    Posts:
    36
    this was exactly what I needed for a label.

    I'm going to play with this now.
    Thanks :)
     
  6. melmonkey

    melmonkey

    Joined:
    Mar 31, 2009
    Posts:
    373
    Thanks Tom!

    I'll give it a whirl.
     
  7. Jaapstudent

    Jaapstudent

    Joined:
    Mar 27, 2009
    Posts:
    24
    HiggyB, thanks u!

    so far one problem less ;)
    but, what i want is a bit difficult, and hard to explane.

    I want to have a button, that appears out of nothing (alpha lerp) that rotates into the right position. together it will be one square.

    each button will be at the corner of THE square and rotates into his position. clockwise rotation.

    and the transformation must stop when the square is completed.
     
  8. Jaapstudent

    Jaapstudent

    Joined:
    Mar 27, 2009
    Posts:
    24
    this is my code so far, but i want the buttons to stop at the position so it form a square. the final positions are, set behind ButtonLeft.... etc.
    How the tackle this problem?


    Code (csharp):
    1.  
    2. var iconLB : Texture2D;
    3. var iconLO : Texture2D;
    4. var iconRB : Texture2D;
    5. var iconRO : Texture2D;
    6. var myStyle : GUIStyle;
    7. var isDaySkybox : boolean = true;
    8. var dayMaterial : Material;
    9. var nightMaterial : Material;
    10. var isFullScreen : boolean = true;
    11. var isWalking : boolean = true;
    12. var iconR : Texture2D;
    13. var isBeginScript : boolean = true;
    14. var isBeginMenu : boolean = false;
    15. private var guiAlpha : float;
    16. var ButtonLeftB = -25.0; // 10.0
    17. var ButtonLeftH = -25.0; // 10.0
    18. var ButtonRightB = 105.0; // 70.0
    19. var ButtonRightH = -25.0; // 10.0
    20. var ButtonDownLeftB = -25.0; // 10.0
    21. var ButtonDownLeftH = 105.0; // 70.0
    22. var ButtonDownRightB = 105.0; // 70.0
    23. var ButtonDownRightH = 105.0; // 70.0
    24.  
    25.  
    26.  
    27. function Start()
    28.     {
    29.         yield GUIFade(0,1,2);
    30.         myStyle = new GUIStyle();
    31.                 //  Camera.main.GetComponent(Skybox).material=nightMaterial;
    32.     }
    33.  
    34. function Update()
    35. {
    36.     ButtonLeftB += (Time.deltaTime * 10.0);
    37.     ButtonLeftH += (Time.deltaTime * 10.0);
    38.     ButtonRightB -= (Time.deltaTime * 10.0);
    39.     ButtonRightH += (Time.deltaTime * 10.0);
    40.     ButtonDownLeftB += (Time.deltaTime * 10.0);
    41.     ButtonDownLeftH -= (Time.deltaTime * 10.0);
    42.     ButtonDownRightB -= (Time.deltaTime * 10.0);
    43.     ButtonDownRightH -= (Time.deltaTime * 10.0);
    44. }  
    45. function OnGUI ()
    46. {
    47.     GUI.color.a = guiAlpha;
    48.        
    49.     if(GUI.Button(Rect(62,62,16,16), iconR,myStyle))
    50.     {
    51.         isBeginScript = !isBeginScript;
    52.    
    53.         if(!isBeginScript)
    54.             isBeginMenu = !isBeginMenu;
    55.             Debug.Log("Menu open");
    56.             GUIFade(0,1,3);
    57.             Debug.Log("fade beginmenu");
    58.  
    59.     }
    60.    
    61.     if(isBeginMenu)
    62.         {
    63.    
    64.    
    65.         if (GUI.Button(Rect(ButtonLeftB,ButtonLeftH,60,60), iconLB,myStyle))
    66.             {          
    67.                         isDaySkybox = !isDaySkybox;
    68.            
    69.                 if (!isDaySkybox)
    70.                     Camera.main.GetComponent(Skybox).material=dayMaterial;
    71.        
    72.                 else
    73.                     Camera.main.GetComponent(Skybox).material=nightMaterial;
    74.        
    75.                 Debug.Log("upper left, Day-Night");
    76.             }
    77.  
    78.         if(GUI.Button(Rect(ButtonDownLeftB,ButtonDownLeftH,60,60), iconLO,myStyle))
    79.             {      
    80.                 isFullScreen = !isFullScreen;
    81.    
    82.                 if(isFullScreen)
    83.                     Screen.SetResolution(640,480, false, 60);
    84.            
    85.                 else
    86.                     Screen.SetResolution(640,480, true, 60);
    87.            
    88.                 Debug.Log("Down left, Full-Normale Screen");
    89.             }
    90.    
    91.         if(GUI.Button(Rect(ButtonRightB,ButtonRightH,60,60), iconRB,myStyle))
    92.             {
    93.                 isWalking = !isWalking;
    94.            
    95.                 if(!isWalking)
    96.                     {
    97.                         Destroy (GetComponent("FPSWalker"));
    98.                         Destroy (GetComponent(CharacterController));
    99.                         gameObject.AddComponent("BoxCollider");
    100.                     }
    101.            
    102.                 else
    103.                     {
    104.                         gameObject.AddComponent("FPSWalker");
    105.                         Destroy(GetComponent(BoxCollider));
    106.                     }  
    107.        
    108.                 Debug.Log("upper right, Walk-Fly");
    109.             }
    110.    
    111.         if(GUI.Button(Rect(ButtonDownRightB,ButtonDownRightH,60,60), iconRO,myStyle))
    112.             {
    113.                 if(!isBeginMenu);
    114.            
    115.                     isBeginMenu = !isBeginMenu;
    116.                     isBeginScript = !isBeginScript;
    117.                     Debug.Log("down right");
    118.             }
    119.     }          
    120. }
    121. function GUIFade (start : float, end : float, length : float) {
    122.    for (i = 0.0; i <= 1.0; i += Time.deltaTime*(1/length)) {
    123.       guiAlpha = Mathf.Lerp(start, end, i);
    124.       yield;
    125.       Debug.Log("Menu alpha");
    126.    }
    127.    guiAlpha = end; // Accounts for Time.deltaTime variance
    128. }
    129.  
    130.  
     
  9. Jaapstudent

    Jaapstudent

    Joined:
    Mar 27, 2009
    Posts:
    24
    problem = no problem.


    just a simple if statement in the update function.
    and a reset in the DownRight button.


    thanks you 4 helping me!
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I'm glad to be of service y'all!
     
  11. Jaapstudent

    Jaapstudent

    Joined:
    Mar 27, 2009
    Posts:
    24
    another question:

    now i have a button that move's into the right position.

    and i found this script, that rotates a button around.

    Code (csharp):
    1. var rotatingButton : Texture2D;
    2. var rotationText : GUIText;
    3. var rotatingGui : GUIStyle;
    4. var theAngle : int = 0;
    5. var thePoint : Vector2 = Vector2.zero;
    6. var horizontalSpeed : float = 400.0;
    7.  
    8. function OnGUI () {
    9.    GUIUtility.RotateAroundPivot (theAngle, thePoint);
    10.    GUI.depth = 1;
    11.    if (GUI.RepeatButton (Rect(10,10,56,56), rotatingButton, rotatingGui)) {
    12.       if (Input.GetAxis ("Mouse X") > 0  theAngle < 130) {
    13.          theAngle += horizontalSpeed * Time.deltaTime;
    14.          print ("mouse moved right: " + theAngle);
    15.       }
    16.       if (Input.GetAxis ("Mouse X") < 0  theAngle > -130) {
    17.          theAngle -= horizontalSpeed * Time.deltaTime;
    18.          print ("mouse moved left: " + theAngle);
    19.       }      
    20.    }
    21.    thePoint = Vector2(38, 38);
    22.    rotationText.text = theAngle.ToString();
    23. }
    the problem is, that i will rotate only if you click the button, hold it and move the mouse around.


    What i want is, i click on a button and then it will rotate around. until the buttons is in the right position. so nothing is moving with the mouse, it must be a automatic transition.
     
  12. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You should look into using coroutines. When the user clicks, you call an animation function that runs through your desired animation routine. Here is a simple example for having a button move, I'll let you extend this to handle rotation if desired.

    Code (csharp):
    1. var MyX : 5.0;
    2.  
    3. function OnGUI () {
    4.  
    5.   var tMyRect = new Rect(MyX, 5.0, 50.0, 20.0);
    6.   if (GUI.Button(tMyRect, "Click me!")) {
    7.     AnimateButton();
    8.   }
    9.  
    10. }
    11.  
    12. function AnimateButton () {
    13.  
    14.   while (MyX < 200.0) {
    15.     MyX += 1.0;
    16.     yield;
    17.   }
    18.  
    19. }
    Again I'm offering _bare_bones_ example scenarios. This button will move to the right and nothing else, you'll have to write things that are more sophisticated in real life, but hopefully the point is coming across. You'll want to do that same in your case, when you want to trigger an animation just call some function that does the animation for you, using yield statements to let the script engine pause that function/let others run to create a nice effect.
     
  13. tomihr2

    tomihr2

    Joined:
    Oct 25, 2010
    Posts:
    29
    Hello,

    how to animate buttons created at runtime in the for loop.

    Thanks
     
  14. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I'm not quite sure what you mean here. Do you mean you have an animation that needs to run in a "for" loop rather than a "while"? The same basic idea of yielding inside the loop should still work. Perhaps you could give a bit more detail if you have some particular effect in mind that you need help with.
     
  15. tomihr2

    tomihr2

    Joined:
    Oct 25, 2010
    Posts:
    29
    Last edited: Nov 2, 2010
  16. Edwige

    Edwige

    Joined:
    Nov 25, 2010
    Posts:
    35
    I am also trying to make Gui elements fade out (for a cartoon like speech bubble system).Problem : all my gui elements (bubbles) are generated inside the same script. So using GUI.color makes every bubbles fade out at the same time. I would also like to control separately the depth of the different bubbles). Is there any way to do it ?
     
    Last edited: Mar 19, 2011
  17. Edwige

    Edwige

    Joined:
    Nov 25, 2010
    Posts:
    35
    I may have found a solution : using GUITexture instead of OnGui fonctions :)
     
  18. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    you can animate a guibutton too, what i do is to create two vars:

    for instance if you want to animate horizontal or vertical movement:
    Code (csharp):
    1.  
    2. var xPos : float;
    3. var yPos : float;
    4.  
    5. function OnGUI(){
    6. ...all the stuffs
    7. }
    8.  
    attach the script to an empty,
    add a new animation to this empty and find the script in the animation list, you will see the two recently created vars..
    add two interval animation curves for those 2 vars, in the frame 0 set the xPos var to 10, then in frame 60 set the xPos to 60 (you can of course use your own values).. and there you go, you have your guiButton animated... it works nice for me..

    note that i´m not using guiTexture anymore because i was having troubles with aspect ratio, the guiTexture went offscreen.... and so..

    hope to help...
    Miguel
     
  19. Filax

    Filax

    Guest

    Joined:
    Mar 30, 2011
    Posts:
    8
    A quick test to put on a camera...

    PS : iTween is just a killing piece of code... Congratulation to the creator!!!

    Code (csharp):
    1. private var currentX01 : float = 0;
    2. private var currentX02 : float = 0;
    3. private var currentX03 : float = 0;
    4.  
    5. private var buttonSize : Vector2 = Vector2(120,40);
    6. private var destX : float = Screen.width/2-60;
    7.  
    8. private var hideX : float = -Screen.width/2;
    9.  
    10. private var buttonRect01 : Rect;
    11. private var buttonRect02 : Rect;
    12. private var buttonRect03 : Rect;
    13.  
    14. private var resetButton : Rect;
    15.  
    16. function Start(){
    17.     buttonRect01 = new Rect(0,50,buttonSize.x,buttonSize.y);
    18.     buttonRect02 = new Rect(0,100,buttonSize.x,buttonSize.y);
    19.     buttonRect03 = new Rect(0,150,buttonSize.x,buttonSize.y);
    20.    
    21.  
    22.     resetButton = new Rect(0,0,30,30);
    23.    
    24. }
    25.  
    26. function OnGUI(){
    27.  
    28.     currentX01 = iTween.FloatUpdate(currentX01,destX, 3);
    29.     currentX02 = iTween.FloatUpdate(currentX02,destX, 2.1);
    30.     currentX03 = iTween.FloatUpdate(currentX03,destX, 1.1);
    31.    
    32.     buttonRect01.x= currentX01;
    33.     buttonRect02.x= currentX02;
    34.     buttonRect03.x= currentX03;
    35.    
    36.     if(GUI.Button(buttonRect01, "New game")){
    37.         destX=hideX;
    38.     }
    39.    
    40.     if(GUI.Button(buttonRect02, "Options")){
    41.         destX=hideX;
    42.     }
    43.    
    44.     if(GUI.Button(buttonRect03, "Quit")){
    45.         destX=hideX;
    46.     }
    47.    
    48.     if(GUI.Button(resetButton, "X")){
    49.         destX=Screen.width/2-60;
    50.     }
    51. }
     
  20. dentedpixel

    dentedpixel

    Joined:
    Jul 15, 2012
    Posts:
    683
    LeanTween now offers some helpful functions to animate GUI Elements.

    Here is an example of scaling a button on click:

    PHP:
        private var bRect:LTRect = new LTRect0010050 );

        function 
    OnGUI(){
         if(
    GUI.Button(bRect.rect“Scale”)){
          
    LeanTween.scalebRectVector2(bRect.rect.widthbRect.rect.height) * 1.30.25, {"ease":LeanTween.easeOutQuad} );
         }
        }
    Also here is an article that gives more advanced examples.
     
  21. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Nice library! :)

    You could also check out eDriven.Gui. Here's the WebPlayer demo showing properties that you can tween.
     
  22. mhicauber

    mhicauber

    Joined:
    Nov 5, 2014
    Posts:
    2
    Hi - How one would do the same thing with a GUILayout element please ?