2017-06-06 42 views
0

我正在从Unity的用户界面转移到NGUI。以统一方式处理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"); 
} 
+0

什么是编译时错误? – kennyzx

+0

@kennyzx没有错误显示。只是不工作。 – armnotstrong

回答

1

最后,我做了这个使用以下脚本(使用UIEventListener)向OnClick添加自定义事件:

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"); 
    } 

需要注意的是,它可能是一个有点傻,在游戏对象的UILable组件传递参数(水平信息)。如果还有其他更优雅的方式,请让我知道。