Author Topic: Which UIButton did the Click come from?  (Read 3001 times)

Dustin_00

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 7
    • View Profile
Which UIButton did the Click come from?
« on: March 10, 2014, 12:24:41 AM »
At run time I find out how many ships there are and Instantiate their buttons and they are all created with an event handler... but the event handler can't tell what button was clicked. Any suggestions?

  1. for (int n = 0; n < ship.Count; n++)
  2. {
  3.         GameObject o = Instantiate(shipButton) as GameObject;
  4.         UIButton button = o.GetComponent<UIButton>();
  5.         button.onClick.Add(new EventDelegate(this, "ButtonShipPressed"));
  6.  
  7.         UILabel label = o.GetComponentInChildren<UILabel>();
  8.         label.text = ship[n].Name;
  9.         // ...and other misc layout stuff...
  10. }
  11.  
  12. public void ButtonShipPressed()
  13. {
  14.         Debug.Log("Which one was clicked?");
  15.         }
  16.  

Agent_007

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 5
  • Posts: 27
    • View Profile
Re: Which UIButton did the Click come from?
« Reply #1 on: March 10, 2014, 02:05:56 AM »
With UIButtonMessage you can do
  1. public void ButtonShipPressed(GameObject caller)
  2. {
  3.     Debug.Log("Button " + caller.name);
  4. }
  5.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Which UIButton did the Click come from?
« Reply #2 on: March 10, 2014, 08:56:31 PM »
Inside the On Click callback, UIButton.current is the caller.

It's the same as with all NGUI classes. UICamera.current, UIProgressBar.current, UIInput.current, etc.

Dustin_00

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Which UIButton did the Click come from?
« Reply #3 on: March 10, 2014, 11:47:33 PM »
My solution from Agent_007's hint:

I create a new script and add in the function:
  1.         public void SelectStartingShipPressed()
  2.         {
  3.                 HostScript.Instance.ButtonShipPressed(this.gameObject);
  4.         }
  5.  

HostScript is the script I actually want to call. Instance is the Singleton for it.
I select the GameObject in the Unity GUI that is the basis for shipButton.
On that I add the above script as a component
Then add UIButtonMessage Component and set the properties:
Target: the shipButton gameObject
Function Name: SelectStartingShipPressed
Trigger: OnClick

That does work. *phew*

ArenMook:
Now that you mention .current, I remember seeing that back in... December? Wow, that long ago? *sigh*

I tried adding into ButtonShipPressed():
  1. UIButton button = UIButton.current;
but button only got NULL assigned to it. As I have a working solution, my interest has faded in exploring further.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Which UIButton did the Click come from?
« Reply #4 on: March 11, 2014, 06:21:20 PM »
UIButtonMessage is a legacy component and should be avoided. UIButton.current is set just before On Click notification gets triggered -- the On Click notification set in inspector under your UIButton.

Anywhere else you can check what's under the mouse by using UICamera.hoveredObject.

Dustin_00

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Which UIButton did the Click come from?
« Reply #5 on: March 12, 2014, 11:07:20 PM »
This works right out of the gate:
  1.                 GameObject go = UICamera.hoveredObject;
  2.  

Which allowed me to clear out the UIButtonMessage components, and then figure out that to get the UIButton.current to work, I just needed to wire up that components OnClick event -- I was using something else (don't ask, I'm really new here).

Refactoring solves everything!

So thanks ArenMook, for the deprecation warning!