Giter Site home page Giter Site logo

dducode / com.dducode.broadcast-messages Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 520 KB

Implements simple broadcast of messages between your classes

License: MIT License

C# 100.00%
design-patterns observer patterns unity observer-design-pattern package-development unity-package

com.dducode.broadcast-messages's Introduction

Broadcast Messages

Setup

  1. Click on the green button "Code"

  2. Then click on copy button, in "HTTPS" tab

  3. In Unity open the Package Manager window (Window/Package Manager)

  4. Then click on plus sign and select "Add package from git URL"

  5. Insert copied link and click on "Add"

  6. After sometime this package will be add to your project

How to use

Messenger is basic class, it sends some messages to all subscribers who subscribe to the broadcast.

For send message without parameters you must write

Messenger.SendMessage<SomeMessage>();

To be able to send SomeMessage, it must be inherits from abstract class Message

public class SomeMessage : Message { }

For send message with some parameters you must write

Messenger.SendMessage(new MessageWith1Arg(arg));
Messenger.SendMessage(new MessageWith2Args(arg1, arg2));

This messages will look like this

public class MessageWith1Arg : Message {

    public readonly object arg;
    
    
    public MessageWith1Arg (object arg) {
        this.arg = arg;
    }
    
}
public class MessageWith2Args : Message {

    public readonly object arg1;
    public readonly object arg2;
    
    
    public MessageWith2Args (object arg1, object arg2) {
        this.arg1 = arg1;
        this.arg2 = arg2;
    }
    
}

To get a messages in your classes they must be subscribed to receive of this messages. Then they can unsubscribes from broadcast

public class FirstYourClass : MonoBehaviour {
    
    private void Awake () {
        Messenger.SubscribeTo<SomeMessage>(DoSomething);
        Messenger.SubscribeTo<MessageWith1Arg>(DoSomething);
    }
    
    
    private void DoSomething (SomeMessage message) {
        // do anything...
    }
    
    
    private void DoSomething (MessageWith1Arg message) {
        if (message.arg is not null) {
            // do anything...
        }
    }
    
    
    private void OnDestroy () {
        Messenger.UnsubscribeFrom<SomeMessage>(DoSomething);
        Messenger.UnsubscribeFrom<MessageWith1Arg>(DoSomething);
    }
    
}
public class SecondYourClass : MonoBehaviour {
    
    private void Awake () {
        Messenger.SubscribeTo<SomeMessage>(DoSomething);
        Messenger.SubscribeTo<MessageWith2Args>(DoSomething);
    }
    
    
    private void DoSomething (SomeMessage message) {
        // do anything...
    }
    
    
    private void DoSomething (MessageWith2Args message) {
        if (message.arg1 is message.arg2) {
            // do anything...
        }
    }
    
    
    private void OnDestroy () {
        Messenger.UnsubscribeFrom<SomeMessage>(DoSomething);
        Messenger.UnsubscribeFrom<MessageWith2Args>(DoSomething);
    }
}

com.dducode.broadcast-messages's People

Contributors

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