gmhevinci / MotionFramework

MotionFramework is unity3d game framework.
MIT License
621 stars 116 forks source link

在事件Handler里Remove掉同事件的另一个Handler会出现异常 #17

Closed transong6778 closed 2 years ago

transong6778 commented 2 years ago

public class TestScript : MonoBehaviour { public class TestEventMsg : IEventMessage, IReference { public string Value;

    // 在回收的时候该方法会被执行
    public void OnRelease()
    {
        Value = null;
    }
}

// Start is called before the first frame update
void Start()
{
    // 创建模块
    MotionEngine.CreateModule<EventManager>();

    EventManager.Instance.AddListener<TestEventMsg>(OnMsg2);
    EventManager.Instance.AddListener<TestEventMsg>(OnMsg1);

    TestEventMsg msg = ReferencePool.Spawn<TestEventMsg>();  
    msg.Value = "hello world";
    EventManager.Instance.SendMessage(msg);
}

void OnMsg1(IEventMessage msg)
{
    if(msg is TestEventMsg temp)
    {
        Debug.Log("msg1 " + temp.Value);

        EventManager.Instance.RemoveListener<TestEventMsg>(OnMsg2);
    }
}

void OnMsg2(IEventMessage msg)
{
    if(msg is TestEventMsg temp)
    {
        Debug.Log("msg2 " + temp.Value);
    }
}

}

现象为触发了两次 msg1

transong6778 commented 2 years ago

印象里GF有处理这个