76
NGUI 3 Support / Re: eventReceiver For All Controls?
« on: July 18, 2012, 01:05:10 AM »
I realized I could add the component via script and get much the same functionality. I don't think there is anything wrong with just doing this...
This example is for a button that starts a level. It is a script on the GUI root that needs to handle the button click. I already have a reference to the UIButton "playButton".
The only thing I don't like about this is having to pass the name of the function instead of a reference. I'd have to try it but I believe you can store a reference and call it even with JavaScript. It isn't a true C# event delegate, but I believe it can be done. Perhaps a new component UIConnectFunction or some such thing... I'm just brainstorming.
This example is for a button that starts a level. It is a script on the GUI root that needs to handle the button click. I already have a reference to the UIButton "playButton".
var messenger = this.playButton.gameObject.AddComponent<UIButtonMessage>();
messenger.target = this.gameObject;
messenger.trigger = UIButtonMessage.Trigger.OnClick;
messenger.functionName = "PlayButtonOnClick";
The only thing I don't like about this is having to pass the name of the function instead of a reference. I'd have to try it but I believe you can store a reference and call it even with JavaScript. It isn't a true C# event delegate, but I believe it can be done. Perhaps a new component UIConnectFunction or some such thing... I'm just brainstorming.