{"id":19210411,"url":"https://github.com/ryanslikesocool/unityfoundation-notificationcenter","last_synced_at":"2025-05-12T19:25:43.790Z","repository":{"id":133710035,"uuid":"486070187","full_name":"ryanslikesocool/UnityFoundation-NotificationCenter","owner":"ryanslikesocool","description":"A simple event system for Unity + C#","archived":false,"fork":false,"pushed_at":"2024-11-16T10:13:27.000Z","size":60,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-12T19:25:37.838Z","etag":null,"topics":["csharp","event-system","notification-center","package","unity"],"latest_commit_sha":null,"homepage":"","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/ryanslikesocool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-04-27T06:18:46.000Z","updated_at":"2024-12-06T08:55:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"12cabb99-df98-4bcb-a278-50e7287907be","html_url":"https://github.com/ryanslikesocool/UnityFoundation-NotificationCenter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FUnityFoundation-NotificationCenter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FUnityFoundation-NotificationCenter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FUnityFoundation-NotificationCenter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanslikesocool%2FUnityFoundation-NotificationCenter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanslikesocool","download_url":"https://codeload.github.com/ryanslikesocool/UnityFoundation-NotificationCenter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253807047,"owners_count":21967282,"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":["csharp","event-system","notification-center","package","unity"],"created_at":"2024-11-09T13:36:03.563Z","updated_at":"2025-05-12T19:25:43.768Z","avatar_url":"https://github.com/ryanslikesocool.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unity Foundation - Notification Center\nA simple event system for Unity + C# inspired by Swift's [NotificationCenter](https://developer.apple.com/documentation/foundation/notificationcenter).\\\nNotification Center falls under the [Unity Foundation](https://github.com/ryanslikesocool/UnityFoundation/) umbrella.\n\n## NOTICE\n**This package is under development and not considered production-ready.**\\\nBreaking changes are common, documentation is incomplete, and support is limited.  Use at your own risk.\n\n## Installation (Unity Package Manager)\n- Select \"Add package from git URL...\" from the plus (+) menu in the package manager window.\n- Paste the package's git url.\n```\nhttps://github.com/ryanslikesocool/UnityFoundation-NotificationCenter.git\n```\n\n## Usage\n```cs\nusing Foundation;\n// NotificationCenter is now available to use!\n```\n\n### Notification Centers\nMore than one notification center may be present in an application, each with its own notifications and observers.\n\nA default shared `NotificationCenter` instance is persistent throughout the lifecycle of a built application.\nIn the editor, this instance is recreated each time Play mode is entered.\n```cs\nNotificationCenter defaultNotificationCenter = NotificationCenter.Default;\n```\n\nWhen in the Unity editor, a shared editor instance becomes available.  The editor notification center is persistent thoughout the lifecycle of the editor application.\n```cs\nNotificationCenter editorNotificationCenter = NotificationCenter.Editor;\n```\n\n### Notifications\nA `Notification.Name` is the primary identifier for notifications.  Each notification should have a unique name to avoid potential collisions.\nIt is required to send and receive notifications, and should be initialized once and cached.\n```cs\nstatic readonly Notification.Name myNotificationName = new Notification.Name(\"my super special notification\");\n```\n\nEvents are posted with associated data, stored in a `Notification`.\nEach `Notification` has the following properties:\n| Type | Name | Description | Optional |\n| - | - | - | - |\n| `Notification.Name` | `name` | The notification's primary identifier. | No |\n| `object` | `sender` | The object that this notification was sent from. | Yes |\n| `object` | `data` | Associated data that the sender provides. | Yes |\n\n### Posting Notifications\nWhen posting a notification, the notification name is required.  Any observers that listen for `myNotificationName` will receive the following notification.  The `sender` and `data` parameters are optional.\n```cs\nvoid PostMyNotification() {\n    // The notification sender can be of any type, but it should ideally be a class.\n    object mySender = this;\n\n    // The notification data can be of any type.\n    object myData = 42; // int\n\n    NotificationCenter.Default[myNotificationName].Post(mySender, myData);\n}\n```\n\n### Observing\nProcessing notification data in observers is as simple as defining a method.\n```cs\n// `MyObserver` will be called every time it receives a notification with the name `myNotificationName`\n// Note the `in` keyword here\nvoid MyObserver(in Notification notification) {\n\t// Safely unwrap the provided data...\n\tif(notification.TryReadData(out int integer)) {\n        Debug.Log(integer); // 42\n\t}\n\n    // ... forcefully unwrap it...\n\tint integer = notification.ReadData(); // 42\n\tMonoBehaviour monoBehaviour = notification.ReadData(); // error!\n\n\t// ... or access it directly.\n\tobject data = notification.data;\n}\n```\n\n### Adding and Removing Observers\nAdd and remove notification observers for receiving events with `NotificationCenter.Default[notificationName].receive += ObserverMethod` and `NotificationCenter.Default[notificationName].received -= ObserverMethod`.  In many cases, this is in an object's initialization and deinitialization stages.\n```cs\nvoid AddMyObserver() {\n    NotificationCenter.Default[myNotificationName].received += MyObserver;\n    // `MyObserver` will now receive notifications from `myNotificationName`.\n}\n\nvoid RemoveMyObserver() {\n    NotificationCenter.Default[myNotificationName].received -= MyObserver;\n    // `MyObserver` will no longer receive notifications from `myNotificationName`.\n}\n```\nA single observer may receive multiple notifications with different names.\n\n### Safety\nMultiple exception types are provided to ensure safety.\n```cs\nvoid MyObserver(in Notification notification) {\n\t// Throw an error for an unexpected notification name...\n\tif (notification.name != myNotificationName) {\n\t\tthrow new NotificationCenter.UnexpectedName(notification, expected: myNotificationName);\n\t}\n\n\t// ... an unexpected sender...\n\tif (notification.sender != mySender) {\n\t\tthrow new NotificationCenter.UnexpectedSender(notification, expected: mySender);\n\t}\n\n\t// ... or unexpected data.\n\tif (!notification.TryReadData(out int integer)) {\n\t\tthrow new NotificationCenter.UnexpectedData(notification, expected: typeof(int));\n\t}\n\n\t// Notification name, sender, and data are all valid.\n\tDebug.Log(integer); // 42\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanslikesocool%2Funityfoundation-notificationcenter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanslikesocool%2Funityfoundation-notificationcenter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanslikesocool%2Funityfoundation-notificationcenter/lists"}