Author Topic: Request: Tutorial-style info for UIEventListener / UIButtonMessage  (Read 19256 times)

Zojak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Issue: Information regarding UIEventListener, UIButtonMessage, and NGUI events can be found in bits and pieces scattered around the web.

NGUI Tutorial: Step 6 says:

Quote
There are others, of course — such as UIButtonPlayAnimation and UIButtonMessage, but I will leave them up to you to explore.

You can also add your own custom event handling to your buttons by attaching a MonoBehaviour that has one or more of the event functions.

I'm sure I'm not alone when I say that some of us are not experts with this stuff! A single tutorial explaining the right ways to use events would be extremely helpful, and surely would cut down on a lot of frustration and repeated questions  :)
« Last Edit: April 16, 2012, 04:11:19 PM by Zojak »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #1 on: April 16, 2012, 01:15:26 PM »
UIEventListener allows you to subscribe to some other game object's event(s). For example, to subscribe to an OnClick event:
  1. UIEventListener.Get(someButtonGameObject).onClick += MyClickListener;
Class Documentation: http://www.tasharen.com/ngui/docs/d5/dfb/class_u_i_event_listener.html

UIButtonMessage lets you trigger a remote function defined by "Function Name" parameter on the game object specified as "Target" when a trigger of your choice is fired.

There is also UIForwardEvents for those coming from an EZGUI background. It's the opposite of UIEventListener. Instead of adding a listener to a remote object, you attach this script to the even trigger (collider) and it will forward events to the target object (manager). I generally suggest using UIEventListener instead. It's faster and cleaner.

Zojak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #2 on: April 16, 2012, 02:07:15 PM »
This is why I requested a tutorial. The information you just posted is not a complete set of instructions on how to use UIEventListener. Adding that line of code you provided to a script does not actually do anything for me.

I readily accept that I'm slow and may need additional help that others do not require :)

So perhaps you can help me figure out this problem - I want to set up a script named TestManager to handle what happens when a button on a menu is clicked. Here's the UI I've created so far (the button I'm trying to set up is highlighted):



Next I added UIButtonMessage to this button. I've set the target to "_TestManager" (game object in the scene with the TestManager.cs script attached) and I've set the function name to "OnButtonCloseClicked."



Then I attempted to set up TestManager.cs like this:

  1. public class TestManager : MonoBehaviour
  2. {
  3.         public UIPanel pMenu;
  4.        
  5.         public GameObject buttonClose;
  6.        
  7.         public bool displayMenu = false;
  8.        
  9.         // Use this for initialization
  10.         void Start ()
  11.         {
  12.                 UIEventListener.Get(buttonClose).onClick += OnButtonCloseClicked;
  13.         }
  14.        
  15.         // Update is called once per frame
  16.         void Update ()
  17.         {
  18.                 if(Input.GetKeyDown(KeyCode.Escape))
  19.                 {
  20.                         displayMenu = !displayMenu;
  21.                         NGUITools.SetActive(pMenu.gameObject, displayMenu);
  22.                 }
  23.         }
  24.        
  25.         void OnButtonCloseClicked()
  26.         {
  27.                 displayMenu = false;
  28.                 NGUITools.SetActive(pMenu.gameObject, displayMenu);
  29.         }
  30. }
  31.  

This does not work and generates errors. Please help me to understand what the problem is.

I imagine I have not properly set up the UIEventListener stuff, because this is the first error I'm getting:

Quote
Assets/Scripts/test/TestManager.cs(21,50): error CS0123: A method or delegate `TestManager.OnButtonCloseClicked()' parameters do not match delegate `UIEventListener.VoidDelegate(UnityEngine.GameObject)' parameters

Additional question: Is GameObject the correct type to use for the buttonClose variable? I see other NGUI elements like UILabel and UIPanel (which I'm using), but there isn't one for UIButton...


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #3 on: April 16, 2012, 02:12:36 PM »
At the very top of the manual page for UIEventListener there is a list of delegate functions:

http://www.tasharen.com/ngui/docs/d5/dfb/class_u_i_event_listener.html

Quote
delegate void    VoidDelegate (GameObject go)
delegate void    BoolDelegate (GameObject go, bool state)
delegate void    FloatDelegate (GameObject go, float delta)
delegate void    VectorDelegate (GameObject go, Vector2 delta)
delegate void    StringDelegate (GameObject go, string text)
delegate void    ObjectDelegate (GameObject go, GameObject draggedObject)

Right below that, you see that OnClick is defined as:

Quote
VoidDelegate    onClick

Which means it's a "VoidDelegate" from the list above.

Quote
delegate void    VoidDelegate (GameObject go)

Note the parameter: "GameObject go".

Your function doesn't have a parameter. Add it, and it will work.

Zojak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #4 on: April 16, 2012, 03:34:51 PM »
If someone asks you if you are a god, you say "yes!"

poulpator

  • Guest
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #5 on: April 17, 2012, 08:53:42 AM »
Hi there,
I purchased NGUI a few days ago and was also wondering how to do what is described here. Glad I found this thread, it answered my questions!
While I do not need a tutorial for this anymore, I do believe there is a very rough step between the very well made starter tutorials and the general documentation
(you know, this one: http://www.tasharen.com/ngui/docs/index.html).
Obviously you can't make a tutorial for every single subject, but it could be interesting to research if there are some common met issues that would justify an additional tutorial or example. (I am wondering myself if I am missing some incredible feature of NGUI just because I am not aware that it exists...)
Then again, there is the forum :p

By the way, NGUI is great and your support for it is awesome!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Request: Tutorial-style info for UIEventListener / UIButtonMessage
« Reply #6 on: April 17, 2012, 06:09:50 PM »
Thanks, and I certainly will keep my eye out for recurring issues. Some of the simple questions I will add to the FAQ.