Giter VIP home page Giter VIP logo

beatthat / binding-behaviours Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 93 KB

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.

License: MIT License

JavaScript 10.05% C# 89.95%
unity unity3d package package-manager events observer observer-pattern notifications callbacks null-check defensive-programming

binding-behaviours's Introduction

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

public class Foo : BindingBehaviour
{
    override protected void BindAll()
    {
        Bind("some-notification-type", () => {
            // this will unbind when Foo::Unbind is called
        });

        Bind<Bar>("some-notification-type-2", (bar) => {
            // you can also bind to a notification that wants to send a param
        });

        Bind<Bar2>((UnityEvent<Bar2>)this.bar2, (bar2) => {
            // also works with Unity events
        });

        Bind<Bar3>((Action<Bar3>)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
    }
}

binding-behaviours's People

Contributors

beatthat avatar

Stargazers

 avatar

Watchers

 avatar  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.