https://github.com/beatthat/binding-behaviours
Base classes for components that can Bind (as listeners) to various types of events and have all those bindings cleanly/safely unbound, either with an explicit call to Unbind or when the component is destroyed.
https://github.com/beatthat/binding-behaviours
callbacks defensive-programming events notifications null-check observer observer-pattern package package-manager unity unity3d
Last synced: 7 months ago
JSON representation
Base classes for components that can Bind (as listeners) to various types of events and have all those bindings cleanly/safely unbound, either with an explicit call to Unbind or when the component is destroyed.
- Host: GitHub
- URL: https://github.com/beatthat/binding-behaviours
- Owner: beatthat
- License: mit
- Created: 2017-08-22T04:39:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-23T00:38:28.000Z (over 5 years ago)
- Last Synced: 2025-06-27T22:05:27.206Z (9 months ago)
- Topics: callbacks, defensive-programming, events, notifications, null-check, observer, observer-pattern, package, package-manager, unity, unity3d
- Language: C#
- Size: 90.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
When you have components that add listeners to other objects--global or otherwise--then it's important (and also tedious) to guarantee that ALL of those listener bindings get cleaned up with the listener component 'unbinds', say is deactivated or destroyed.
This package contains base classes for both MonoBehaviours and StateMachineBehaviour that provide methods to bind UnityEvents, Actions, Notifications, etc. and have those bindings auto unbind on a common call.
## Install
From your unity project folder:
npm init --force # only if you don't yet have a package.json file
npm install --save beatthat/binding-behaviours
The package and all its dependencies will be installed under Assets/Plugins/packages.
In case it helps, a quick video of the above: https://youtu.be/Uss_yOiLNw8
## Usage
```csharp
public class Foo : BindingBehaviour
{
override protected void BindAll()
{
Bind("some-notification-type", () => {
// this will unbind when Foo::Unbind is called
});
Bind("some-notification-type-2", (bar) => {
// you can also bind to a notification that wants to send a param
});
Bind((UnityEvent)this.bar2, (bar2) => {
// also works with Unity events
});
Bind((Action)this.bar3, (bar3) => {
// also works with csharp events
});
}
void OnDestroy()
{
// all of the above bindings automatically unbind on destroy
// or when Foo::Unbind is called explicitly
}
}
```