Author Topic: Examples of Reading which button OnClick'd  (Read 5779 times)

TomA6

  • Guest
Examples of Reading which button OnClick'd
« on: August 31, 2012, 02:18:12 PM »
Hi

Do you have any examples that read in which buttons were pressed and printed to Unity's Console?

I see there are notes in the docs on how to create your own custom events script that you would attach to the would be button.

Can I see an example where by I can access UIButton events (or whatever) from say a remote script (main game script)? Like by holding a reference to the NGUI GameObject (UI Root) and then reading in UI events triggered by a receiver or UICamera component. Is there a Singleton NGUI EventListener or something?

Thanks,
Tom

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Examples of Reading which button OnClick'd
« Reply #1 on: August 31, 2012, 02:26:29 PM »
For example:

You can create static class called i.e. GameGlobals with static variable

public static GameObject lastPressedButton;

, and then, in your button script add into OnClick method ... GameGlobals.lastPressedButton = this;

Now you have access to last pressed object ( button) from all classes.




TomA6

  • Guest
Re: Examples of Reading which button OnClick'd
« Reply #2 on: August 31, 2012, 04:05:11 PM »
Very cute but I don't want to do all the button polling all on my self now... :o

TomA6

  • Guest
Re: Examples of Reading which button OnClick'd
« Reply #3 on: August 31, 2012, 04:27:12 PM »
Ok I think I figured it out...
Assuming the UIButton name is Button_Ok is this case...
  1. UIEventListener.Get(GameObject.Find("Button_Ok")).onClick += (sender) =>
  2.                 {
  3.                         print("Hello I'm ("+sender.name+")");
  4.                 };

One for the FAQ...