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.
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.
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;
}
}