# How to use NGUI with Unity

I feel like Im missing something really simple, but Ive read about all the events NGUI can generate from an object and just dont get where to recive (or handle) the event. Do I just attach it to a script on the object?

Yup.

You can also look into add-on frameworks like NData, which is a MVVM data manager which can simplify a lot of UI code (and complicate other UI code; use the right tool for the job and all that jazz).

• Im using the free version of ngui for the moment, so I probably wont invest in Ndata just yet. So on my object I just add a script with something like self.onClick = myFunction or is that wrong? All of the NGUI examples just seem to be using other NGUI functions and scripts. My main issue is how to I tie the UI to my game – ProtoJazz Nov 13 '12 at 20:47
• I finaly get it. I knew it would be something so simple but I just couldnt figure it out. For anyone else who has no idea what they are doing, it really is just as easy as adding a script to your object For mine I was using Java Script, so my object has a script with function OnClick(){ Application.LoadLevel("main");} – ProtoJazz Nov 15 '12 at 16:44

The easiest way to handle UI Events in NGUI is to use the UIEventListener class. All you have to do is call it like this:

UIEventListener.Get(buttonObj).onClick += YourClickCallback;


Some of the NGUI widget scripts have delegates to handle events, I just modified the rest to let me supply event handlers - ie. added a buttonClickDelegate and buttonHoldDelegate to UIButton