Author Topic: How do I procedurally invoke / fake a button press?  (Read 3744 times)

DonKirkland

  • Guest
How do I procedurally invoke / fake a button press?
« on: April 18, 2012, 11:22:42 PM »
Heya.

I'm new to NGUI - I played with the evaluation version and just purchased the full version - am having much fun with it!

I'm relatively new to C#, as most of my previous 'coding' experience is scripting for levels, etc. in lua or similar.

Is there an easy way to emulate a button press on a button object (e.g. a message or series of messages I can send the GameObject or similar) in order to fake a user input procedurally?

The two scenarios I'm looking at are:
1. A UI panel with a timer and a default action. If the timer expires, the default action occurs - but I'd also like to invoke all the FX, audio, etc. on the button to make it clear to the user what just happened.

2. A menu button on the Android that has a button which is 'pressed' if the back button is pressed on the device. Again, I want to make it clear and obvious to the user exactly what the game is doing. My exact scenario is a pause menu that has three options: resume, restart and quit. The quit button is the one I want to invoke when the back button is pressed.

Thanks

=Don

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I procedurally invoke / fake a button press?
« Reply #1 on: April 18, 2012, 11:25:46 PM »
Easiest solution?

  1. buttonGameObject.SendMessage("OnClick");

DonKirkland

  • Guest
Re: How do I procedurally invoke / fake a button press?
« Reply #2 on: April 19, 2012, 12:37:55 AM »
This was my first attempt, but from what I can tell that only invokes the OnClick behavior.

I'm after something that will fake a full button press - my second attempt was as follows:

  1. IEnumerator FakeButton(GameObject myButton)
  2. {
  3. myButton.SendMessage("OnPress",true);
  4. var testTime = Time.realtimeSinceStartup;
  5. while (Time.realtimeSinceStartup - testTime < 0.5) yield return new WaitForEndOfFrame();
  6. myButton.SendMessage("OnPress",false);
  7. }

I was rather hoping the various button-y components on the button GameObject would respond, but this did nothing as far as I could tell.  :(

My goal is to not just have the resulting behavior of the button, but also any tweening, offsets and other behaviors typically associated with a user press followed by a user release within the button collision volume.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I procedurally invoke / fake a button press?
« Reply #3 on: April 19, 2012, 12:44:04 AM »
Do you queue this function as StartCoroutine(FakeButton(myButton));? It's not going to work if you just call it directly. Sending OnPress message works fine. You should also use yield WaitForSeconds(0.5f); instead.

DonKirkland

  • Guest
Re: How do I procedurally invoke / fake a button press?
« Reply #4 on: April 19, 2012, 12:52:34 AM »
And no, I was forgetting to explicitly make it a co-routine.

*embarassed*

Thanks :)

The reason I'm not using WaitForSeconds is because I want it to reliably be independent of what Time.timeScale is doing. Being UI and all.  ;D

Final code was:

  1. IEnumerator FakeButtonPress (GameObject uiButton)
  2. {
  3.                 uiButton.SendMessage("OnPress",true); //emulate user press on button
  4.                 var pressTime = Time.realtimeSinceStartup;
  5.                 while (Time.realtimeSinceStartup - pressTime < 0.2) yield return new WaitForEndOfFrame(); //wait for 0.2 seconds of real time
  6.                 uiButton.SendMessage("OnPress",false); // emulate user release of button
  7.                 uiButton.SendMessage("OnClick"); //to actually invoke what the button invokes
  8.         }

Which was called by
  1. StartCoroutine("FakeButtonPress",myButtonObject);

where myButtonObject is a GameObject reference to the button.

(edit: for anyone else who followed this and wanted to see the end result)
« Last Edit: April 19, 2012, 12:59:48 AM by DonKirkland »