Kar*_*arz 0 c# delegates event-listener unity-game-engine
我在Unity遇到问题:
我尽量让事件侦听器与委托,和我从教程的代码,但我有一个错误,当我尝试添加委托方法为其他类方法:"无法隐式转换类型void' to
Metronome.OnTickEvent".
这是我与代表的班级:
public delegate void OnTickEvent();
public event OnTickEvent onTick;
IEnumerator coroutineMetronome() {
if (CustomTimer.manager.timerState) {
for (;;) {
nextTick += delay;
yield return new WaitForSeconds(nextTick - Time.time);
onTick(); // I call the delegate method here
}
}
}
Run Code Online (Sandbox Code Playgroud)
......那就是事件接收器类:
protected virtual void Start ()
{
manager = this as T;
Metronome.manager.onTick += OnSynchronization(); // Here is the bug line
}
protected void OnSynchronization() {
Debug.Log("coucou");
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助 !
方法名称后面不需要括号:
Metronome.manager.onTick += OnSynchronization;
Run Code Online (Sandbox Code Playgroud)
使用括号表示您希望首先调用该方法,然后将结果添加到事件中.
更正后的代码是简短版本
Metronome.manager.onTick += new OnTickEvent(OnSynchronization);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1153 次 |
最近记录: |