Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

RepeatButton true/false

Discussion in 'Immediate Mode GUI (IMGUI)' started by claustopholt, Mar 21, 2009.

  1. claustopholt

    claustopholt

    Joined:
    Mar 17, 2009
    Posts:
    9
    I am scratching my head about RepeatButton, used in OnGUI() in Unity 2.5 Windows.

    It seems that when pressed, it returns true only every other frame! So, the following code:

    Code (csharp):
    1. void OnGUI()
    2. {
    3.   if (GUI.RepeatButton(new Rect(10, 450, 190, 50), "Button"))
    4.   {
    5.     Debug.Log("True");
    6.   }
    7.   else
    8.   {
    9.     Debug.Log("False");
    10.   }
    11. }
    returns "False" until I press the RepeatButton, and then returns "True", "False", "True", "False", ... until I let go of the RepeatButton.

    Bug or very weird feature?

    / Claus
     
  2. Doug Smith

    Doug Smith

    Joined:
    Mar 27, 2009
    Posts:
    20
    This seems to be problem on all Unity versions. I'm using Unity 2.5 for mac and Unity iPhone and I'm experiencing the same thing.
    here's how I got around it:

    Code (csharp):
    1.  
    2. var buttonImage : Texture2D;
    3. var doStuff : boolean = false;
    4.  
    5.  
    6. function Update()
    7. {
    8.     if (doStuff)
    9.     {
    10.         // Do this while the GUI RepeatButton is down.
    11.     }
    12.     else
    13.     {
    14.         // Or do this when the button is released.
    15.     }
    16. }
    17. function OnGUI()
    18. {
    19.     if (GUI.RepeatButton (Rect (10,10,50,50), buttonImage))  
    20.     {
    21.         doStuff = true;
    22.     }
    23.     else
    24.     {
    25.         doStuff = false;
    26.     }
    27. }
    28.  
    29.  
    You just have to get out of the RepeatButton() block if you want it to work.
    Now if you Debug.Log in Update() you'll see that the results won't toggle unexpectedly. doStuff will be true while the button is down.
    At least it works for me. :?
     
  3. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    The repeat button gets executed twice per frame as long as the user has the mouse button clicked and held down on the gui button. It's used for game behavior that makes the user have to hold down a button to execute game behavior continuously until the user lets go of the button.

    It would be better if you explained what you want to achieve. Because to me it sounds as if you want to click a button to make something true, then click it again to make something false (and back and forth). Is this right? If so, then just use a regular button.

    I'm certain I missed something about all this now that I think about it. :?
     
  4. claustopholt

    claustopholt

    Joined:
    Mar 17, 2009
    Posts:
    9
    So it's actually expected behavior? It doesn't say anything about this in the manual :)

    I wanted a "hold down and increase firing speed until released" button, and for that the RepeatButton is useless if it gets called twice per frame with true+false.

    At any rate, I just ended up using GUITexture and mouse events instead.

    Thanks!
     
  5. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    The repeat button works just fine for me. The problem you had with your script is that you had the true/false statements inside of OnGUI, which gets called by the engine twice per frame. Normally, this doesn't affect gameplay (does it affect your gameplay?) but it will shoot of true/false/true/false etc inside the console.

    Doug got it right by putting the work code inside of an Update function (which only gets updated once per frame). That's how it works.
     
  6. claustopholt

    claustopholt

    Joined:
    Mar 17, 2009
    Posts:
    9
    Thanks for the help and explanations, guys.

    One thing is how it works, but I still say it's somewhat weird behavior -- I mean, WHY would it return false when pressed every other frame? Perhaps just to keep people like me from erroneously putting gameplay-related code into a GUI event handler :)

    Thanks again!
     
  7. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    I think the logic behind the true/false toggling is actually to prevent other sorts of bugginess. If the GUI is called twice per frame, then if it returned true both times it would call whatever gameplay code was in the if block twice per frame, too. But since all the game logic works on a 1-execution-per-frame basis, calling anything but the GUI twice per frame could be a recipe for trouble. Therefore, one true return per frame.

    The easiest solution would probably just be to get rid of the else block and just have it do what you want it to when you press the button. It seems strange to me that you would want it to execute any code whenever the button is *not* being pressed anyway. If you want it to do something all the time and not do it when you press the button, put that function in an Update instead and add some logic in the RepeatButton's if block to temporarily stop that action.
     
  8. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Exactly. Just put action code inside an update function (although game behavior wise it isn't necessary, you can still have it in OnGUI, but the processing power required would probably double since it's called twice per frame).
     
  9. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    I am using Doug's work around for the repeat button, but am having a strange result. It seems that the button does not work unless I hover my mouse over it, only then does touch seem to work on the iphone.

    Any ideas?

    Thanks,

    Greg
     
  10. Doug Smith

    Doug Smith

    Joined:
    Mar 27, 2009
    Posts:
    20
    Greg got it... and just in case someone is needing the answer from this tread...

    The RepeatButton() does not work properly with the iPhone remote connection. It does however work perfectly when the game is installed on the phone.
     
  11. GodModeTech

    GodModeTech

    Joined:
    Jun 21, 2010
    Posts:
    71
    It is useful to be able to determine a mouse or touch up on the button. And you would check the "else" you are talking about. I guess I will try to check that at update().