Giter VIP home page Giter VIP logo

unimediator's Introduction

UniMediator

BCH compliance

UniMediator is a Unity package that enables you to easily use the Mediator pattern in Unity. This library is inspired by MediatR and works in a very similar way. If you've used MediatR before you already know how to use UniMediator.

What problem does UniMediator solve?

UniMediator allows your MonoBehaviours to invoke methods on each other without having a dependency on or a reference to the objects it is invoking those methods on. Target methods are resolved based on their method signature and do not need to explicitly subscribe or unsubscribe.

Installation

This repository follows Unity's package format. To add UniMediator to your Unity project, locate your Project manifest (called manifest.json, in the Packages folder under the root folder of your Unity Project) and add this repository as a dependency as below:

{
  "dependencies": {
    "com.tharinga.unimediator": "https://github.com/tharinga/UniMediator.git"
  }
}

The package should automatically be installed and be visible in the Package Manager window.

Usage

Add UniMediator to your scene by clicking Window > UniMEdiator > Install

You only need to add UniMediator to the scene that is the entry point to your game, however adding it to multiple scenes will not cause any problems.

UniMediator supports two types of messages:

  1. IMulticastMessage targets 0 or many handler methods with a void return type. Listeners for this type of message would typically be objects with a transient life cycle.

  2. ISingleMessage<T> targets 1 and exactly 1 handler method with a return type of T. Listeners for this type of message would typically be services with a Singleton life cycle.

Using IMulticastMessage

An IMulticastMessage is created as follows:

public class ExampleMessage : IMulticastMessage  
{  
    public int Value { get; set; }  
  
    public ExampleMessage(int value)  
    {  
        Value = value;  
    }  
}

A handler for this message is created as follows:

public class ExampleHandler : MonoBehaviour,   
    IMulticastMessageHandler<ExampleMessage>  
{  
    public void Handle(ExampleMessage message)  
    {  
        Debug.Log(message.Value);  
    }  
}

You do not need to register the Handle method with UniMediator, it is resolved automatically.

You can now dispatch messages from anywhere in your project by creating a message object and dispatching it through the mediator as follows:

var message = new ExampleMessage(123);
Mediator.Publish(message);

That's it, no further configuration is required. UniMediator will automatically unsubscribe the handler if the GameObject it is attached to is destroyed.

Using ISingleMessage<T>

An ISingleMessage<T> is created as follows:

public class ExampleMessage : ISingleMessage<string>  
{  
    public string Message { get; set; }  
  
    public ExampleMessage(string message)  
    {  
        Message = message;  
    }  
}

A handler for this message is created as follows:

public class ExampleMessageHandler : MonoBehaviour,   
    ISingleMessageHandler<SingleMessage, string>  
{  
    public string Handle(ExampleMessage message)  
    {  
        return message.Message + "Pong";  
    }  
}

You can now dispatch messages and get a return value from anywhere in your project by creating a message object and dispatching it through the mediator as follows:

var message = new ExampleMessage("Ping");
var result = Mediator.Send(message);

Debug.Log(result); // prints "PingPong"

Note that there must be 1 and only 1 handler for each ISingleMessage<T>. Because returning value types is supported, there is no sane default value to return when no listener exists, so a UniMediatorException will be thrown if there is no handler in the scene.

Keeping your objects humble

The examples above use a static convenience method. If you want to keep your objects humble you can instead inject the IMediator interface.

For example if you use Zenject you can bind UniMediator as follows:

Container.Bind<IMediator>()
    .To<MediatorImpl>()
    .FromComponentInHierarchy()
    .AsSingle();

And you would then inject UniMediator as follows:

private IMediator _mediator;

[Inject]
public void Construct(IMediator mediator)
{
    _mediator = mediator;    
}

unimediator's People

Contributors

tharinga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unimediator's Issues

Question: How do you monitor for new instantiations of GameObjects?

This is a very cool library. I have been reading through the code though and I don't currently see where you attach to newly created game objects. How is that done? (is it done?)

I can see the ScanScene method, and how it loops through all the currently active monobehaviours and adds them to the list, but I can't find anything that listens for new object instantiations in the active scene.

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.