C# 为什么要从Unity委托事件中删除事件?

C# 为什么要从Unity委托事件中删除事件?,c#,unity3d,interface,C#,Unity3d,Interface,我目前正在审查代码 在复习过程中,我收到了一个问题 这是接收事件的部件的代码内容 EventManager.cs: using System.Collections; using System.Collections.Generic; using UnityEngine; using System; namespace wp.events { delegate void WordEventDelegate(object obj, EventArgs e); // 델리게이트 선언

我目前正在审查代码

在复习过程中,我收到了一个问题

这是接收事件的部件的代码内容

EventManager.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace wp.events
{
    delegate void WordEventDelegate(object obj, EventArgs e); // 델리게이트 선언
    class EventManager : WordGameSingleton<EventManager>  // 싱글톤 상속받음
    {
        public event  WordEventDelegate wordeSendEvent; // 이벤트 델리게이트 선언

        public void dispatchEvent(object obj, EventTypes type, Hashtable data = null) // 이벤트 수신 
        {
            GameEvent evt = new GameEvent ();
            evt.obj = obj; // 받은 이벤트의 객체
            evt.eventType = type; // 받은 이벤트의 타입
            evt.evetData = data; // 받은 이벤트의 데이터

            wordeSendEvent (this, evt);
        }

        public void addEvent(WordEventDelegate wedlistener) // 이벤트를 추가함
        {
            wordeSendEvent += wedlistener;
        }

        public void removeEvent(WordEventDelegate wedlistener) // 이벤트를 삭제함
        {
            wordeSendEvent -= wedlistener;
        }
    }
}
注册事件作为接口实现,对象和事件参数设置为参数

例如

public enum EventTypes
{
    GAME_INIT,
    ...
}
interface IWordGame
{
    void onHandleEvent(object sender, EventArgs e);
}
public void init()
{
    EventManager.Instance.addEvent(onHandleEvent);
}
当游戏开始时,它会在init()函数中添加一个事件

例如

public enum EventTypes
{
    GAME_INIT,
    ...
}
interface IWordGame
{
    void onHandleEvent(object sender, EventArgs e);
}
public void init()
{
    EventManager.Instance.addEvent(onHandleEvent);
}
我明白,即使添加事件。 但是,我不知道为什么再次删除添加的事件

下面的代码显示了该部分

public void onHandleEvent(object sender, EventArgs e)
{
    GameEvent evt = e as GameEvent;
    Debug.Log(">> event type : " + evt.type);
    switch(evt.type)
    {
        case EventTypes.INTRO_STAR:
            EventManager.Instance.removeEvent(onHandleEvent);
            break;
    }
}
我很好奇。我不明白为什么要删除添加的事件

你能澄清这一部分吗


作为初学者,请帮助我。

此模式用于只听一次任何特定事件。例如,在您的情况下初始化游戏。如果您不从事件中取消订阅处理程序,则每当触发事件时,它都会继续接收调用

以下是您的代码的作用:

  • 订阅活动
  • 处理第一个事件
  • 在第一次呼叫后取消订阅活动,因为目的已实现
  • 这将订阅/注册EventManager的
    WordeSendent

    public void init()
    {
        EventManager.Instance.addEvent(onHandleEvent); // subscribing
    }
    
    case EventTypes.INTRO_STAR:
        EventManager.Instance.removeEvent(onHandleEvent); // unsubscribing
    break;
    
    onHandleEvent是
    wordSendEvent
    的侦听器。首次触发此事件时,将调用onHandleEvent

    并且它将取消订阅EventManager的
    WordeSendent

    public void init()
    {
        EventManager.Instance.addEvent(onHandleEvent); // subscribing
    }
    
    case EventTypes.INTRO_STAR:
        EventManager.Instance.removeEvent(onHandleEvent); // unsubscribing
    break;
    

    希望这有帮助。

    事件的开始是否意味着我已经注册了一个事件?我希望您的代码审查也提出了这样一个观点,即该代码使用Java命名约定而不是C#的命名约定。另外,根据约定,*上名为
    的方法用于引发事件,而不是处理事件。您的
    onHandleEvent
    显然正在处理该事件。它应该正确地被称为
    HandleEvent