Skip to content

Instantly share code, notes, and snippets.

@kmnk
Created July 20, 2017 05:26
Show Gist options
  • Save kmnk/f4997c7858634cc1105a7536cfed3895 to your computer and use it in GitHub Desktop.
Save kmnk/f4997c7858634cc1105a7536cfed3895 to your computer and use it in GitHub Desktop.
train Zenject
using UnityEngine;
using System;
using UniRx;
using Zenject;
public class TapCounterInstaller : MonoInstaller<TapCounterInstaller>
{
public override void InstallBindings()
{
Container.Bind<TapCounterView>().FromMethod(CreateTapCounterView);
Container.BindInterfacesAndSelfTo<TapCounterModel>().AsSingle();
Container.BindInterfacesAndSelfTo<TapCounterPresenter>().AsSingle();
}
public TapCounterView CreateTapCounterView(InjectContext context)
{
var obj = Container.InstantiatePrefabResource("Popup/TapCounter");
var t = obj.GetComponent<RectTransform>();
t.parent = this.transform;
return obj.GetComponent<TapCounterView>();
}
}
// Should create other script file
public class TapCounterModel
{
IntReactiveProperty count = new IntReactiveProperty(0);
public IObservable<int> Count { get { return count.ToReadOnlyReactiveProperty(); } }
public void CountUp()
{
count.Value = count.Value + 1;
}
}
// Should create other script file
public class TapCounterPresenter : IInitializable, IDisposable
{
[Inject]
TapCounterModel _model;
[Inject]
TapCounterView _view;
public void Initialize()
{
_view.CountButtonTapped.Subscribe(_ => _model.CountUp());
_model.Count.Subscribe(c => _view.UpdateCount(c));
}
public void Dispose()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment