Author Topic: Using UIEventTrigger to get mouse down on button  (Read 7268 times)

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Using UIEventTrigger to get mouse down on button
« on: December 05, 2014, 09:54:57 AM »
Hi,

I'm using an EventDelegate to subscribe to an onPress event, but it only seems to be for the initial press (on frame). How do I use the EventDelegate to invoke the method whilst the button is being pressed as opposed to just the initial press?

  1. EventDelegate.Add(m_decreasePriceButtonTrigger.onPress, HandleDecreasePriceButtonPressed);

I've read you can use OnPress() attached to the same GO as your button, but I really want to just have a reference to the button in another script (not on the GO of the button).

Thanks for any help :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using UIEventTrigger to get mouse down on button
« Reply #1 on: December 05, 2014, 04:05:33 PM »
Inside the HandleDecreasePriceButtonPressed:
  1. void HandleDecreasePriceButtonPressed (bool isPressed)
  2. {
  3.     if (isPressed) StartCoroutine("DoStuff");
  4.     else StopCoroutine("DoStuff");
  5. }
  6.  
  7. IEnumerator DoStuff()
  8. {
  9.     for (;;)
  10.     {
  11.         // do something here
  12.         yield return null;
  13.     }
  14. }

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Using UIEventTrigger to get mouse down on button
« Reply #2 on: December 07, 2014, 01:30:26 PM »
Hi Aren,

I tried to change the method signature of HandleDecreasePriceButtonPressed() to include the bool isPressed, but I didn't think EventDelegate methods could have params?

It gives an error to say the method signature is incorrect.

If I need to add that method call inside the HandleDecreasePriceButtonPressed() I don't understand where the EventDelegate gets the bool isPressed from.

Thanks for the help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using UIEventTrigger to get mouse down on button
« Reply #3 on: December 09, 2014, 12:40:20 PM »
Don't use an EventDelegate in this case. use UIEventListener.Get(targetGameObject).onPress += HandleDecreasePriceButtonPressed;