Code Monkey home page Code Monkey logo

unityfoundation-notificationcenter's Introduction

Unity Foundation - Notification Center

A simple event system for Unity + C#.

NOTICE

This package is not considered production-ready. Breaking changes are common and support is limited. Use at your own risk.

Installation (Unity Package Manager)

  • Select "Add package from git URL..." from the plus menu in the package manager window.
  • Paste the package's git url.
https://github.com/ryanslikesocool/UnityFoundation-NotificationCenter.git

Usage

NotificationCenter creates a default shared instance the first time it is accessed in C#.

Notifications

A Notification.Name is the primary identifier for notifications. It is required to send and receive notifications, and should be initialized once and cached.

static readonly Notification.Name myNotificationName = new Notification.Name("my super special notification");

Events are passed around with associated data, stored in a Notification. Each Notification has the following properties:

Property Description
Notification.Name name The notification's primary identifier.
object sender The object that this notification was sent from. (Optional)
object data Associated data that the sender provides. (Optional)

Adding and Removing Observers

Add and remove notification observers for recieving events with NotificationCenter.Default.AddObserver and NotificationCenter.Default.RemoveObserver. In most cases, this would be in an object's initialization and deinitialization stages.

void AddMyObserver() {
    // notifications will be posted to `MyObserver` after this is called.
    NotificationCenter.Default[myNotificationName].received += MyObserver;
}

void RemoveMyObserver() {
    // notifications will no longer be posted to `MyObserver` after this is called.
    NotificationCenter.Default[myNotificationName].received -= MyObserver;
}

Posting Notifications

When posting a notification, the notification name is required. Any observers that listen for myNotificationName will recieve the following notification. The sender and data are not required.

void PostMyNotification() {
    // the notification sender can be of any type, but it should be a class.
    object mySender = this;

    // the notification data can be of any type.
    object myData = 42; // int

    NotificationCenter.Default[myNotificationName].Post(mySender, myData);
}

Observing

// this function will be called every time it recieves a notification with the name `myNotificationName`
// note the `in` keyword here
void MyObserver(in Notification notification) {
	// safely unwrap the provided data...
	if(notification.TryReadData(out int integer)) {
        Debug.Log(integer); // 42
	}

    // ...forcefully unwrap it...
	int integer = notification.ReadData(); // 42
	MonoBehaviour mb = notification.ReadData(); // error!

	// ...or access it directly
	object data = notification.data;
}

Safety

void MyObserver(in Notification notification) {
	if (notification.name != myNotificationName) {
		throw new NotificationCenter.UnexpectedName(notification, expected: myNotificationName);
	}
	if (notification.sender != mySender) {
		throw new NotificationCenter.UnexpectedSender(notification, expected: mySender);
	}
	if (!notification.TryReadData(out int integer)) {
		throw new NotificationCenter.UnexpectedData(notification, expected: typeof(int));
	}

	// notification name, sender, and data are all valid
	Debug.Log(integer); // 42
}

unityfoundation-notificationcenter's People

Contributors

ryanslikesocool avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.