0

I'm transferring from Unity's UI to NGUI.

Formerly, I could add listeners to button with following scripts:

button.GetComponentInChildren<Button>().onClick.AddListener(() =>
{
    //codes that could be triggered by click the button. 
});

But when changing to NGUI, I can't make it work by:

EventDelegate.Set(go.GetComponent<UIButton>().onClick, OnButtonClickActive);

private void OnButtonClickActive()
{
   Debug.Log("active button clicked");
}
armnotstrong
  • 8,605
  • 16
  • 65
  • 130

1 Answers1

1

Finally, I made this work by adding a custom event to OnClick with the following script (using UIEventListener):

UIEventListener.Get(go).onClick += OnButtonClickActive;

And the event handler is defined as follows:

private void OnButtonClickActive(GameObject go)
    {
        int level;
        int.TryParse(go.GetComponentInChildren<UILabel>().text, out level);
        Debug.Log(level + "active button clicked");
        ApplicationModel.CurrentLevel = ApplicationModel.Levels[level - 1];
        SceneManager.LoadScene("PlayScene");
    }

Note that, it might be a little bit silly to pass the parameter (level info) with the UILable component in the gameobject. If there is other more elegant way, please let me know.

armnotstrong
  • 8,605
  • 16
  • 65
  • 130