以编程方式统一处理ngui的按钮事件 [英] handle ngui's button event programmatically in unity
本文介绍了以编程方式统一处理ngui的按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从Unity的UI转移到NGUI.
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.
});
但是当更改为NGUI时,我无法通过以下方式使其工作:
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");
}
推荐答案
最后,我通过使用以下脚本将自定义事件添加到 OnClick
(使用 UIEventListener
):
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");
}
请注意,通过游戏对象中的 UILable
组件传递参数(级别信息)可能有点愚蠢.如果还有其他更优雅的方法,请告诉我.
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.
这篇关于以编程方式统一处理ngui的按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文