You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中AddListener绑定EventManager委托事件报错,求正确格式

Hey there! Let's break down why you're hitting that error and fix your code step by step.

What's Causing the Error?

Your issue stems from two key misunderstandings:

  1. You can't directly call an event like a regular method: EventManager.OnGenerateNewDriftSetClicked is defined as an event, not a standard method. Events only support += (subscribe) or -= (unsubscribe) operations from outside their defining class—direct invocation like you tried isn't allowed.
  2. Unity Button's onClick expects a parameterless delegate: The AddListener method for UI Buttons requires an Action (a delegate with no parameters), but your event uses a delegate that takes two parameters (int and string). These don't match directly.
Fixed Code for GameManager.cs

Here's the corrected way to hook up your button click to trigger the event:

public void AddListeners() 
{
    // Use a lambda expression to wrap the event trigger logic, matching Button's parameterless requirement
    newDriftBtn.onClick.AddListener(() => 
    {
        // Safely invoke the event with your parameters (null check avoids crashes if no subscribers exist)
        EventManager.OnGenerateNewDriftSetClicked?.Invoke(driftNumSteps, driftName);
    });
}

Key Notes:

  • The ?.Invoke() syntax is crucial: If no scripts have subscribed to OnGenerateNewDriftSetClicked (e.g., if GenerateNewDrift is disabled), calling Invoke() directly would throw a null reference error. The ?. operator checks if the event has subscribers first.
  • Your existing code in EventManager.cs and GenerateNewDrift.cs is correct! The +=/-= logic in GenerateNewDrift properly subscribes/unsubscribes to the event, and it will receive the driftNumSteps and driftName parameters when the event is triggered.
Optional Cleanup: Encapsulate Event Triggering

For better encapsulation, you can add a dedicated method in EventManager to handle triggering the event, instead of invoking it directly from outside the class:

Updated EventManager.cs

public class EventManager : MonoBehaviour 
{
    public delegate void GenerateNewDriftSetHandler(int steps, string title);
    public static event GenerateNewDriftSetHandler OnGenerateNewDriftSetClicked;

    // Public static method to safely trigger the event
    public static void TriggerGenerateNewDriftSet(int steps, string title)
    {
        OnGenerateNewDriftSetClicked?.Invoke(steps, title);
    }
}

Updated GameManager.cs

public void AddListeners() 
{
    newDriftBtn.onClick.AddListener(() => 
    {
        EventManager.TriggerGenerateNewDriftSet(driftNumSteps, driftName);
    });
}

This keeps all event-related logic contained within EventManager, making your code easier to maintain and follow.

内容的提问来源于stack exchange,提问作者greyBow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:59:10