{"id":13786206,"url":"https://github.com/Bian-Sh/UniEventSystem","last_synced_at":"2025-05-11T22:30:43.358Z","repository":{"id":107845562,"uuid":"177171490","full_name":"Bian-Sh/UniEventSystem","owner":"Bian-Sh","description":"This is a generic Event-Bus for Unity ","archived":false,"fork":false,"pushed_at":"2019-10-10T09:01:05.000Z","size":245,"stargazers_count":30,"open_issues_count":0,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T01:04:45.105Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.jianshu.com/p/bf82beb41f7f","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bian-Sh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-03-22T16:08:08.000Z","updated_at":"2024-11-28T05:52:21.000Z","dependencies_parsed_at":"2024-01-17T05:14:02.577Z","dependency_job_id":"a6479ddc-d335-4510-a9a7-29e6b45f61f2","html_url":"https://github.com/Bian-Sh/UniEventSystem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bian-Sh%2FUniEventSystem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bian-Sh%2FUniEventSystem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bian-Sh%2FUniEventSystem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bian-Sh%2FUniEventSystem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bian-Sh","download_url":"https://codeload.github.com/Bian-Sh/UniEventSystem/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253645086,"owners_count":21941311,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T19:01:11.673Z","updated_at":"2025-05-11T22:30:42.809Z","avatar_url":"https://github.com/Bian-Sh.png","language":"C#","funding_links":[],"categories":["GamePlay","Message Bus"],"sub_categories":["HUD"],"readme":"# UniEventSystem\n# 通用事件总线 For Unity\n\n# 简述\n* 事件总线虽然是全局的,但这个事件系统使用了 Enum 对象来装载事件的枚举值,保证了事件链中 key 值的 唯一性、多样性、高辨识度和可扩展性（枚举的优势）。\n* 相较于常见框架中以 int 值做为事件链的 key值的做法，避免了必须为所有枚举指定一个独一无二的 int值 的操作,这个操作似乎不利于可持续发展？\n* 这个事件系统 API 风格回归Unity，使用起来更加亲切：\n\n|API|Description|\n|---|------|\nAddListener(xx) | 订阅事件\nDelListener(xx) | 移除事件\nRemoveAllListener() | 移除所有事件\nInvoke(xx) |分发事件\n\n # 使用方法：\n \n \u003e 1.订阅事件 （演示了监听不同枚举描述的不同事件种类）\n```\nEventManager.AddListener(StylusEvent.Press, OnPointPress);\nEventManager.AddListener(StylusEvent.Release, OnPointRelease);\n\nEventManager.AddListener(ScriptEvent.Amount, OnScriptMounted); \nEventManager.AddListener(ColorEvent.ChangeTo, OnColorChangeRequired);\nEventManager.AddListener(UIEvent.PopUp, OnTipsReceive);       \n```\n\n\u003e 2.分发事件（链式风格）\n```\n  EventManager.Allocate\u003cStylusEventArgs\u003e() //从Pool 里面分配参数类型实例\n    .Config( StylusEvent.Press,gameObject,target.gameObject) //配置参数类型实例\n    .Invoke(); //分发事件\n                \n  // 演示订阅其他类型的事件\n  EventManager.Allocate\u003cColorEventArgs\u003e()\n    .Config(ColorEvent.ChangeTo, gameObject, c)\n    .Invoke();\n\n```\n\n\u003e 3.移除事件\n```\nEventManager.DelListener(StylusEvent.Enter, OnPointEnter); //移除时，可以指定事件类型\nEventManager.DelListener(OnPointExit);//移除时也可以不指定事件类型\nEventManager.DelListener(StylusEvent.Enter);//移除时也可以通过指定 事件种类 来移除这一类的所有事件\nEventManager.RemoveAllListener(); //可以使用该方法全部移除\n```\n\n\u003e 4. 处理事件：\n\n继承 BaseEventArgs 实现自定义事件信息类，可以在事件中传递丰富的信息,但请进行必要的里氏转换哈。\n```\n private void OnScriptMounted(BaseEventArgs obj)\n {\n     ScriptEventArgs args = obj as ScriptEventArgs;\n     //do something \n }\n\n private void OnTipsReceive(BaseEventArgs obj)\n {\n     UIEventArgs args = obj as UIEventArgs;\n     //do something \n }\n private void OnPointPress(BaseEventArgs obj)\n {\n     StylusEventArgs args = obj as StylusEventArgs;\n     //do something \n }\n\n```\n# 动画演示\n![](https://upload-images.jianshu.io/upload_images/3600713-b2ae5147fd58b5dd.gif?imageMogr2/auto-orient/strip)\n\n# 友情提示\n* 事件总线是静态的，所以请养成在 OnDestory 方法中移除事件的好习惯，总线是大家的，自己屁股自己擦，不留后患给大家。\n（啥子后患哦？ 就是莫名其妙的报 null 呗~）\n* 这些事件都是 Oneshot 事件，就是说事件传递的参数中的各种引用会在执行完毕后被回收。\n\n# 关于拆箱装箱：\n虽然这个事件总线的设计思路虽然很棒，但前提是装箱不耗性能的情况下。\n\n实际上，虽然在事件总线内部逻辑中未发现装箱拆箱操作，但是在配置参数类和订阅事件的时候会各进行一次装箱操作。\n\n如下图：\n\n![Config函数使用时的装箱](doc/01.png)\n\n![Config函数使用时的装箱](doc/02.png)\n\n**但是也不用那么担心：**\n* 首先，事件的订阅是可枚举出来的，也就是说不会产生那么大的数量级的装箱。\n\n* 其次，事件的执行也是条件触发式的，而不是说每时每刻都在装箱。\n\n* 再次，现在的设备性能，运算水平，这点毛毛雨的性能损耗也上不了台面，反而更应该确认在使用类似 Debug.LogFormat 这样的API的GC问题。\n\n* 最后，相对于即得的优惠，和明面上的装箱性能损耗，我还是挺心安的，仅供参考。\n\n**解决方案**\n短期还是准备将每一个 枚举指导一个独一无二的 int值\n为了便利起见，这个值应该由编辑器工具自动生成\n当然，args 类也能自动生成一个模板也是极好的，后续只需要在模板的基础上增加关心的字段即可\n\n只希望吧，这个装箱损耗能被解决掉就好咯~~\n\n# 我的简书：\n[Unity 3D 教你打造自己的EventSystem(事件总线) - 简书](https://www.jianshu.com/p/bf82beb41f7f)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBian-Sh%2FUniEventSystem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBian-Sh%2FUniEventSystem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBian-Sh%2FUniEventSystem/lists"}