Author Topic: Assistance with UIEventListener  (Read 3002 times)

Christopher

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Assistance with UIEventListener
« on: December 03, 2014, 09:22:24 PM »
Hello,

I was hoping someone could help me with what I want to do with UIEventListener. I've created a "Dialogue" Prefab in my game and it has images/text/buttons associated with it. I can change all those at will, but I want to be able to dynamically change the function pointed to by the UIEventListner so I can make it generic and call a function in the script which created the dialogue window. Something like this...

In Town.CS
>inn.transform.GetComponent<dialogue_class>().active_listen_button1(inn_function);
> void inn_function(GameObject button)
> {
> inn.transform.GetComponent<dialogue_class>().close_listen_button1(inn_function);
> // Do Stuff - Button was Clicked and Listener is now off
> }

In dialogue_class.CS
>public void active_listen_button1(string target_function)
> { UIEventListener.Get(inn.transform.FindChild("Button - Action 1").gameObject).onClick    += target_function; }
>public void close_listen_button1(string target_function)
> { UIEventListener.Get(inn.transform.FindChild("Button - Action 1").gameObject).onClick    -= target_function; }

If anyone could help, I'd appreciate it. Been struggling with this issue for a couple hours and I can't seem to make it work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Assistance with UIEventListener
« Reply #1 on: December 04, 2014, 04:23:11 AM »
UIButton has a built-in event, onClick. To subscribe to it, use EventDelegate.Add or EventDelegate.Set(button.onClick, YourDelegateFunction); UIEventTrigger script has all sorts of delegates if you need more.

UIEventListener is a generic delegate-based approach you can also use if you like (EventDelegate is an NGUI delegate instead that supports serialization and inspector functionality). UIEventListener is not editable through inspector, and remains a code-based solution.

I don't quite understand your code, or why you are creating extra functions... but if you do, why pass a string? Pass a delegate instead. Replace "string" with "UIEventListener.OnClick".

Christopher

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Assistance with UIEventListener
« Reply #2 on: December 04, 2014, 01:29:41 PM »
Thanks - I was able to pass it by explicitly stating it was a UIEventListener.VoidDelegate instead of the string;
I was creating the extra functions to pass a lot of parameters to the gameobject and make it reusable in multiple scenes.