我已经使用Winforms很长时间了,对事件处理有一定的经验,但是在Unity开发方面还比较新手。我的项目只是想在Android上运行一些C#代码,所以没有必要使用超级高效的解决方案,只需要一个能工作的就可以。
事件处理程序声明:
public event EventHandler OnShow, OnHide, OnClose;
事件处理程序调用:
Debug.Log("OnShow");
if (OnShow != null)
{
Debug.Log("OnShow_Firing");
OnShow(this, new EventArgs());
}
else{
Debug.Log("OnShow_empty");
}
在同一游戏对象中但另一个脚本中添加的事件处理程序。
void Awake(){
Debug.Log("Awake");
this.gameObject.GetComponent<windowScript>().OnShow += OnShowCalled;
}
private void OnShowCalled(object o,EventArgs e)
{
Debug.Log("OnShowCalled");
}
我的调试输出如下:
- "Awake"
- "OnShow"
- "OnShowFiring"
但是 "OnShowCalled" 没有被执行,Unity 的控制台中没有任何异常。
我尝试使用 EventArgs.Empty
而非评论中提到的 new EventArgs()
,但对我的问题没有影响。
希望得到任何帮助。
EventArgs.Empty
而不是new EventArgs()
。 - Olivier Jacot-DescombesEventArgs.Empty
。 - leAthlonthis.gameObject.GetComponent<WindowScript>()
有点让我感到奇怪。有可能是因为你没有订阅调用OnShow()
的对象吗? - Brandon