Giter VIP home page Giter VIP logo

ext-unity-event-bus's Introduction

Unity C# Event Bus

EventBus

Description

This EventBus system provides a way to create decoupled architectures in Unity projects. It allows communication between different parts of an application without requiring direct references.

Code Structure

This EventBus system contains several C# classes residing in the Scripts\EventBus directory:

  1. EventBus.cs - Main EventBus class that provides static functions for registering, deregistering, and triggering custom events.

  2. EventBinding.cs - IEventBinding interface and class definition for EventBinding, which is used to bind functions to events.

  3. Events.cs - IEvent interface and sample code, which shows how to define custom events.

  4. PredefinedAssemblyUtil.cs - Utility class for locating assemblies and finding types within them. See Unity Documentation.

  5. EventBusUtil.cs - Static initialization methods and additional utilities used for EventBus.

Example Usage

The usage generally works like:

public struct PlayerEvent : IEvent {
    public int health;
    public int mana;
}

EventBinding<PlayerEvent> playerEventBinding;

void OnEnable() {    
    playerEventBinding = new EventBinding<PlayerEvent>(HandlePlayerEvent);
    EventBus<PlayerEvent>.Register(playerEventBinding);

    // Can Add or Remove Actions to/from the EventBinding
}

void OnDisable() {
    EventBus<PlayerEvent>.Deregister(playerEventBinding);
}

void Start() {
    EventBus<PlayerEvent>.Raise(new PlayerEvent {
        health = healthComponent.GetHealth(),
        mana = manaComponent.GetMana()
    });    
}

void HandlePlayerEvent(PlayerEvent playerEvent) {
    Debug.Log($"Player event received! Health: {playerEvent.health}, Mana: {playerEvent.mana}");
}

YouTube

Watch the tutorial video here

You can also check out my YouTube channel for more Unity content.

Installation and Setup

Since this is a Unity-centric project, you will need to have Unity installed on your system. The EventBus codebase is entirely C# and conforms to the .NETFramework v4.7.1 standards.

To use these scripts in your project, please place these scripts in your Scripts or a related directory in the Unity editor.

Contributions

Contributions are always welcome. You can contribute by improving the EventBus codebase, enhancing its features, or providing suggestions.

Inspired by

This project takes inspiration from the following open source projects:

ext-unity-event-bus's People

Contributors

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