在AddListener中格式化委托事件的正确方法

时间:2018-05-29 16:01:16

标签: c# unity3d events delegates

我正在学习如何在我的代码中实现委托,并且我在逻辑方面遇到了一些麻烦。我想使用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);
}

0 个答案:

没有答案