我正在学习如何在我的代码中实现委托,并且我在逻辑方面遇到了一些麻烦。我想使用AddListener方法在按钮单击时调用事件,但我收到错误
EventManager.OnGenerateNewDriftSetClicked事件只能出现在左侧 手边+ =或 - =(除非在类型' EventManager'中使用)
代码行newDriftBtn.onClick.AddListener(EventManager.OnGenerateNewDriftSetClicked(driftNumSteps, driftName));
格式化此行的正确方法是什么?谢谢!
GameManager.cs
public void AddListeners()
{
newDriftBtn.onClick.AddListener(EventManager.OnGenerateNewDriftSetClicked(driftNumSteps, driftName));
}
EventManager.cs
public class EventManager : MonoBehaviour
{
public delegate void GenerateNewDriftSetHandler(int steps, string title);
public static event GenerateNewDriftSetHandler OnGenerateNewDriftSetClicked;
}
GenerateNewDrift.cs
private void OnEnable()
{
EventManager.OnGenerateNewDriftSetClicked += InitDrift;
}
private void OnDisable()
{
EventManager.OnGenerateNewDriftSetClicked -= InitDrift;
}
public void InitDrift(int driftNumSteps, string driftName)
{
string driftID = GenerateDriftID();
CreateTable(driftID);
PopulateTable(driftID, driftName, driftNumSteps);
}