我正在从Unity的UI转移到Ngui。
以前,我可以将侦听器添加到按钮中,并带有以下脚本:
button.GetComponentInChildren<Button>().onClick.AddListener(() =>
{
//codes that could be triggered by click the button.
});
但是,当更改为ngui时,我无法通过:
来工作EventDelegate.Set(go.GetComponent<UIButton>().onClick, OnButtonClickActive);
private void OnButtonClickActive()
{
Debug.Log("active button clicked");
}
最后,我通过使用以下脚本将自定义事件添加到OnClick
(使用UIEventListener
(:
UIEventListener.Get(go).onClick += OnButtonClickActive;
,事件处理程序的定义如下:
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");
}
请注意,使用GameObject中的UILable
组件传递参数(级别信息(可能有些愚蠢。如果还有其他更优雅的方式,请让我知道。