Search Unity

cycle animation with gui buttons.

Discussion in 'Scripting' started by hosse, Mar 10, 2011.

  1. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    Hey all, I'm a bit of a scripting noob, and was hoping I cold get some scripting help,

    I have a 5 frame animation which I want to link to 2 GUI buttons, Previous and Next. The end results needs to be so you can manually cycle back and forth the animation set with the 2 buttons.

    But, when you get to the last frame, the Next button does not bring you back to the first frame, it stays on the last and visa versa with the previous button.

    How do I create this guys?

    Thanks.!
    -Hosse
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You should put the textures into an array and use an integer variable to select which texture you want from the array. Increment the integer when the Next button is clicked and decrement it when Previous is clicked. You can then use Mathf.Clamp to make sure the integer doesn't go off the ends of the array:-
    Code (csharp):
    1. var textures: Texture2D[];
    2. var index: int;
    3. var prevButtonRect: Rect;
    4. var nextButtonRect: Rect;
    5. var textureRect: Rect;
    6.  
    7.  
    8. function OnGUI() {
    9.     if (GUI.Button(prevButtonRect, "Prev")) {
    10.         index = Mathf.Clamp(index - 1, 0, textures.Length - 1);
    11.     }
    12.    
    13.     if (GUI.Button(nextButtonRect, "Next")) {
    14.         index = Mathf.Clamp(index + 1, 0, textures.Length - 1);
    15.     }
    16.    
    17.     GUI.DrawTexture(textureRect, textures[index]);
    18. }
     
  3. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    I should of mentioned, its 3d geometry that is animated. The geometry has a different position in each of the 5 frames. SO the 2 gui buttons will cycle through the 5 different positions.

    This make any sense?

    Thanks!
    -Hosse
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    do the exact same thing he mentioned except instead of textures you need an array of transforms, and instead of setting the texture you need to position your camera at the transform

    EDIT: ok that was vague.

    Code (csharp):
    1.  
    2. var textures: Texture2D[];
    3. var index: int;
    4. var prevButtonRect: Rect;
    5. var nextButtonRect: Rect;
    6. var cameraT : Transform;
    7.  
    8.  
    9. function OnGUI() {
    10.     if (GUI.Button(prevButtonRect, "Prev")) {
    11.         index = Mathf.Clamp(index - 1, 0, textures.Length - 1);
    12.     }
    13.    
    14.     if (GUI.Button(nextButtonRect, "Next")) {
    15.         index = Mathf.Clamp(index + 1, 0, textures.Length - 1);
    16.     }
    17.    
    18.     cameraT.position = textures[index].position;
    19.     cameraT.rotation= textures[index].rotation;
    20. }
    21.  
     
    Last edited: Mar 10, 2011
  5. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    is there no way i can import my model with 5 frames of animation and the buttons cycle through the anim frame by frame?

    -Hosse