Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: Christopher 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.
-
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".
-
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.