Coder Social home page Coder Social logo

beamablezenject's Introduction

Beamable and Zenject

Zenject is a powerful Dependency Injection (DI) tool for Unity. Beamable also uses an internal DI system. This package integrates Beamable's DI system into the Zenject Installer format so you can continue to use Zenject while taking full advantage of Beamable.

Getting Started

Before you get started, you will need a Unity project with Zenject installed. Beamable must also be installed. In the Unity Package Manager, add a package via git-link. for com.beamable.solana and use the following git-link.

https://github.com/beamable/BeamableZenject.git?path=/Packages/com.beamable.zenject#latest

Note: the end of the link includes the version number. You view the available versions by looking at this repositories git tags. The link above will install the latest deployed package, but if you wish to pin to a specific version, you replace "latest" with any of the available tags.

Using UPM, import the demo project to see Zenject in action.

Usage

If you have any Zenject container, you can bind a BeamContext to that container.

var ctx = BeamContext.Default;
Container.BindBeamableContext(ctx);

There is a scriptable object installer called BeamableInstaller available that will automatically install a BeamContext into a Zenject container. This is how the sample works.

Once Beamable has been installed in a Zenject container, any of the Beamable services registered through Beamable's internal DI system can be accessed with Zenject's injection, via the [Inject] attribute, or through construction.

Look at the ZenjectExampleBehaviour for an example. Here is an except.

public class ZenjectExampleBehaviour : MonoBehaviour
{
    [Header("Scene references")]
    public TextMeshProUGUI dbidLabel;
    public TextMeshProUGUI currencyLabel;
    
    [Inject]
    private BeamContext ctx;
    
    [Inject]
    private PlayerInventory inventory;
    
    async void Start()
    {
        dbidLabel.text = $"DBID: (loading)";
        
        // only need this because the sample doesn't have a loading scene.
        await ctx.OnReady; // I wish we could enforce this timing into the Zenject cycle :/ 

        dbidLabel.text = $"DBID: {ctx.PlayerId}";
        inventory.GetCurrencies().OnDataUpdated += UpdateDisplay;
    }

    void UpdateDisplay(List<PlayerCurrency> currencies)
    {
        Debug.Log("updating display");
        var newText = string.Join("\n", currencies.Select(curr => $"{curr.CurrencyId}={curr.Amount}"));
        currencyLabel.text = newText;
    }

}

beamablezenject's People

Contributors

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