Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使架构 Architecture 内部事件处理器也可通过接口定义,并能一键注册/注销 #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Hocchi01
Copy link

@Hocchi01 Hocchi01 commented Aug 31, 2023

模仿 CommunityToolkit.Mvvm 中 ObservableRecipient 和 IRecipient 的使用方式。
现在可通过如下方式声明架构内事件处理器并注册:

public class Player : ViewController, IController,
    IOnEventInArchitecture<AEvent>,
    IOnEventInArchitecture<BEvent>
{
    public void OnEventInArchitecture(AEvent e) { \* handle AEvent ... *\ }
    public void OnEventInArchitecture(BEvent e) { \* handle BEvent ... *\ }

    void Awake()
    {
        this.RegisterAllEventInArchitecture(this); // 一键注册
    }
}

好处:通过类头部快速知道该类注册了哪些事件;一键注册减少了重复书写的样板代码
其中 RegisterAllEventInArchitecture 方法默认使事件处理器在 gameObject 销毁后自动注销,通过参数 isAutoUnregister 可控制。
UnRegisterAllEventInArchitecture 方法用于一键注销这些事件处理器。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant