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");
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.