{"id":13728725,"url":"https://github.com/gwiazdorrr/EventBetter","last_synced_at":"2025-05-08T00:32:22.282Z","repository":{"id":41542566,"uuid":"160364443","full_name":"gwiazdorrr/EventBetter","owner":"gwiazdorrr","description":"A Unity messaging/event system for the lazy","archived":false,"fork":false,"pushed_at":"2022-10-11T19:26:11.000Z","size":100,"stargazers_count":97,"open_issues_count":0,"forks_count":12,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-05-15T06:34:29.085Z","etag":null,"topics":["unity","unity3d"],"latest_commit_sha":null,"homepage":null,"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/gwiazdorrr.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}},"created_at":"2018-12-04T13:47:50.000Z","updated_at":"2024-04-28T08:30:08.000Z","dependencies_parsed_at":"2023-01-19T21:16:11.611Z","dependency_job_id":null,"html_url":"https://github.com/gwiazdorrr/EventBetter","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/gwiazdorrr%2FEventBetter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gwiazdorrr%2FEventBetter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gwiazdorrr%2FEventBetter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gwiazdorrr%2FEventBetter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gwiazdorrr","download_url":"https://codeload.github.com/gwiazdorrr/EventBetter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":213757088,"owners_count":15634118,"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":["unity","unity3d"],"created_at":"2024-08-03T02:00:48.978Z","updated_at":"2024-08-03T02:02:26.699Z","avatar_url":"https://github.com/gwiazdorrr.png","language":"C#","funding_links":[],"categories":["C#"],"sub_categories":[],"readme":"# EventBetter\nA Unity pubsub/messaging/event system for the lazy. Here's why: \n- Raising events is alloc-free.\n- No interfaces to implement, no base types to derive from.\n- No initialization, no clean-up.\n- No message codes.\n- Coroutine/async friendly.\n- One source file (everything else here is just for the test environment).\n- Clear and readable.\n\n# TL;DR:\nCopy [EventBetter.cs](Assets/Plugins/EventBetter/EventBetter.cs) anywhere to your project or add `https://github.com/gwiazdorrr/EventBetter.git` in `Package Manager` (`Add package from git URL...`). The API you need to know is `EventBetter.Listen` and `EventBetter.Raise`. Done!\n\nExample:\n\n```cs\nclass TextMessage\n{\n    public string text;\n}\n\nclass SimpleProducer : MonoBehaviour\n{\n    void Update()\n    {\n        EventBetter.Raise(new TextMessage() { text = \"Hello World!\" });\n    }\n}\n\nclass SimpleConsumer : MonoBehaviour\n{\n    void Awake()\n    {\n        EventBetter.Listen(this, (TextMessage msg) =\u003e Debug.Log(msg.text, this));\n    }\n}\n```\n\nThere's no need to unlisten/unsubscribe from anything.\n\n# How does it work\n\nThe first parameter in `Listen` is the listener; as long as it is alive, the handler (the second parameter) will be invoked whenever there is a `Raise` called with a matching type. If the listener gets destroyed (with `UnityObject.Destroy` or when changing scenes), the handler will not get invoked any more and all the references will get cleaned up no later than in the next LateUpdate.\n\nIt is possible thanks to `UnityEngine.Object` being the main citizen in the Unity world - it has a native representation with lifetime controlled entirely by the engine. There's no need to use `WeakReference`, `ConditionalWeakTable` and boilerplate like \"unsubscribe\" to avoid leaks, just keep track of the native parts.\n\n# More examples\n\nMaybe you like async/await more?\n\n```cs\nclass SimpleConsumerAsync : MonoBehaviour\n{\n    async void Awake()\n    {\n        var msg = await EventBetter.ListenAsync\u003cTextMessage\u003e();\n        Debug.Log(msg.text, this);\n    }\n}\n```\n\nOr maybe you'd rather stick with good old coroutines?\n```cs\nclass SimpleConsumerCoro : MonoBehaviour\n{\n    void Awake()\n    {\n        StartCoroutine(Coro());\n    }\n\n    IEnumerator Coro()\n    {\n        var listener = EventBetter.ListenWait\u003cTextMessage\u003e();\n        yield return listener;\n        Debug.Log(listener.First.text, this);\n    }\n}\n```\n \nBack to the basic `Listen`, maybe you want to stop listening after the first message arrives?\n```cs\nEventBetter.Listen(this, (TextMessage msg) =\u003e Debug.Log(msg.text, this), once: true);\n```\n\nOr listen only if the listening script is active and enabled?\n```cs\nEventBetter.Listen(this, (TextMessage msg) =\u003e Debug.Log(msg.text, this), exculdeInactive: true);\n```\n\nIf you are not in a `MonoBehaviour` and still want to use EventBetter, use:\n```cs\nIDisposable listener = EventBetter.ListenManual( (TextMessage msg) =\u003e Debug.Log(msg.text, this) );\n// ...\nlistener.Dispose();\n``` \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgwiazdorrr%2FEventBetter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgwiazdorrr%2FEventBetter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgwiazdorrr%2FEventBetter/lists"}