Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: poolts on May 09, 2014, 03:00:20 PM

Title: UIEventListener.Get or OnClick Delegate
Post by: poolts on May 09, 2014, 03:00:20 PM
I've seen this topic covered in a few different places in the forum, but wanted a bit more clarification on when to use a certain one and their performance implications (just for good general practice really).

So:

I've read that UIEventListener.Get is used for remote events (i.e. in a config script where you assign delegates to several buttons). Therefore I guess it maybe beneficial if you have a lot of buttons that do the same thing to do:

  1.        
  2. void OnEnable()
  3. {
  4.     foreach(UIButton b in m_buttons)
  5.     {
  6.         UIEventListener.Get(b.gameObject).onClick += HandleButton;
  7.     }
  8. }
  9.  

Although minimal, would the b.gameObject (which I guess is effectively b.GetComponent<GameObject>()) cause a problem if the loop was carried out on a large number of buttons?

My first thought when using buttons was to do:

  1. void OnEnable()
  2. {
  3.     myButton.onClick += HandleMyButtonClick();
  4. }
  5.  

However, that wasn't the right way to do it, as onClick uses a List<EventDelegate> so my next step was to try myButton.onClick.Add(Delegate), which also wasn't right  :-[

So, I came full circle and thought why not just add the monobehaviour scripts to the on click property in the inspector (something I was a bit reluctant to do, as I thought it may use reflection or SendMessage and thus affect performance).

So question time:

Is it just best to use the on click property in the inspector and select a delegate? Will this affect performance (via SendMessage / Reflection, if it uses them that is)?

If so should I use UIEventListener.Get instead? Seems like I should just use the inspector property the majority of the time, but would like to know either way just for best practice when setting up my UI.

Thanks in advance  :)
Title: Re: UIEventListener.Get or OnClick Delegate
Post by: ArenMook on May 09, 2014, 10:05:50 PM
EventDelegate.Add(button.onClick, YourFunc);
Title: Re: UIEventListener.Get or OnClick Delegate
Post by: poolts on May 10, 2014, 04:46:48 AM
Thanks :) Are there any performance differences between using EventDelegate.Add(button.onClick, YourFunc) in script or just hooking up the delegate in the inspector (i.e. does it use SendMessage, reflection or any costly calls) ?
Title: Re: UIEventListener.Get or OnClick Delegate
Post by: ArenMook on May 10, 2014, 11:59:11 PM
Event delegate doesn't use SendMessage unless it's falling back to it because of the platform (WP8/WSA/Flash).

Both EventDelegate.Add and inspector-chosen delegates are the same thing.