Author Topic: UIEventListener confusion  (Read 6894 times)

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
UIEventListener confusion
« on: June 05, 2012, 04:12:14 AM »
I'm trying to figure out how to use the UIEventListener.

Basically my game object needs to listen for a button being pressed down and released. I'm new to events and delegates and previously followed THIS TUTORIAL VIDEO but since converting my UI to NGUI i'm lost in a few places.

Where do i put the event listener? Is it on my game object that I want to react to the button press or is it on it's own game object in the scene?

I was thinking I didn't need the UIEventListener as the script on my game object IS the event listener if I add the code?

Do I need to use UIButtonMessage with UIEventListener or are the 2 separate?

I've confused myself with this now and I'm sure i'm making it a lot more complicated than it needs to be. I thought the eventlistener went on all game objects that were "listening" for events. An event manager was used to send the events & the buttons sent their status to the manager?

Fbary

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 25
    • View Profile
Re: UIEventListener confusion
« Reply #1 on: June 05, 2012, 05:28:17 AM »
Attach a script with a FunctionA() in it to the gameObjectA.
Attach UIButtonMessage to the gameobjectB.
Set gameObjectA as target of UIButtonMessage, and write "FunctionA" in function name field.

Now gameObjectA will send a message wich will activate that function in gameObjectB.

GameObjectA can be a manager, and every other object can speak with it to activate its functions =)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener confusion
« Reply #2 on: June 05, 2012, 06:01:35 AM »
When using UIEventListener you will generally do it from within whatever manager or event handler class you have:

  1. UIEventListener.Get(gameObjectToListenTo).onClick += YourClickFunction;

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: UIEventListener confusion
« Reply #3 on: June 05, 2012, 06:23:50 AM »
Thanks, seems to be working with:

  1. public void onButtonPressed (GameObject go,bool isDown){
  2.                
  3.         buttonPressed = true;  
  4.                
  5.         }


but I want buttonPressed to = false when the button isn't pressed, how can I read the isDown = false?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener confusion
« Reply #4 on: June 05, 2012, 01:40:34 PM »
What's OnButtonPressed? NGUI has OnPress, not OnButtonPressed.

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: UIEventListener confusion
« Reply #5 on: June 06, 2012, 01:41:25 AM »
What's OnButtonPressed? NGUI has OnPress, not OnButtonPressed.

OnButtonPressed is my click function that basically checks a box in the inspector so i can see if it's working or not.

But I can get it to only work when the button is held down. How do i read the isDown true and false?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener confusion
« Reply #6 on: June 06, 2012, 02:28:32 AM »
  1. UIEventListener.Get(gameObjectToListenTo).onPress += YourPressFunction;

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: UIEventListener confusion
« Reply #7 on: June 06, 2012, 02:36:53 AM »
Here's what I have that works, but only on press, not on release. Note that I need 2 buttons that will do the same thing, hence the 2 buttons in the script:

  1. public class test : MonoBehaviour {
  2.        
  3.         public GameObject theButton;
  4.         public GameObject the2ndButton;
  5.         public bool buttonPressed = false;
  6.  
  7.         // Use this for initialization
  8.         void Start () {
  9.  
  10.                 UIEventListener.Get(theButton).onPress += onButtonPressed;
  11.                 UIEventListener.Get(the2ndButton).onPress += onButtonPressed;
  12.         }
  13.        
  14.        
  15.         public void onButtonPressed (GameObject go,bool isDown){
  16.                
  17.         buttonPressed = true;  
  18.                
  19.         }
  20. }

When the button is pressed buttonPressed = true but when the button is released it doesn't go back to false.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener confusion
« Reply #8 on: June 06, 2012, 02:38:42 AM »
... You never check the "isDown" property, how do you expect to know when the button was pressed and when it was released?

Change it to:

  1. void onButtonPressed (GameObject sender, bool isDown)
  2. {
  3.     buttonPressed = isDown;        
  4. }

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: UIEventListener confusion
« Reply #9 on: June 06, 2012, 02:46:25 AM »
Thanks! That's what i was asking for.  So obvious now that it's pointed out.  ::) Works a treat!