- Home /
NGUI Eventdelegate Add passing a function with parameters
I'm trying to figure out how to add methods to a slider's onChange event the problem is I have parameters in the method and how to do this properly this seems to change in different versions of NGUI I'm on 3.5.7.(I'm on 3.5.7 still for the company I'm at has custom scripts with NGUI so we haven't bothered updating yet) I'm trying it this way:
EventDelegate.Add(slider.onChange, delegate() { Method(slider, maxVal, val); });
It adds the method that I can see in the editor but it comes out as something like (Awake)M__3 in the editor the number may vary. I also tried adding to the Eventdelegate's parameters list but the variables available to me are different from the ones available from the codes samples I've seen.
The only way I can get it to work is to hard code 6 methods without parameters and pass them to their appropriate sliders onChange event.
Any suggestions before I try and update NGUI?
Answer by VesuvianPrime · Sep 15, 2014 at 07:19 PM
Hey Nikar
In your example, the solution would be:
EventDelegate.Add(slider.onChange, () => Method(slider, maxVal, val));
Just keep in mind any parameter changes won't be passed to the delegate.
I think the nicest way to handle these delegates is to create a base class with some virtual methods:
[RequireComponent(typeof(UIScrollView))]
public abstract class UIScrollViewHelper : MonoBehaviour
{
public UIScrollView scrollView
{
get { return GetComponent<UIScrollView>() }
}
protected virtual void Awake()
{
EventDelegate.Add(slider.onChange, OnChange);
}
protected virtual void OnChange()
{
}
}
Then you can simply inherit from your helper class and override methods as required:
public class MyScrollView : UIScrollViewHelper
{
protected override void OnChange()
{
base.OnChange();
// Do OnChange code here
}
}
Your answer
Follow this Question
Related Questions
NGUI UICenterOnChild, find out what is centered 1 Answer
How to redirect on other screen by clicking a button? 0 Answers
How to show to image when button is clicked using c# with touch event 0 Answers
Display ngui label over gameobject 1 Answer
Why does NGUI uiinput class don't use touchscreenkeyboard ? 0 Answers