Skip to content

Instantly share code, notes, and snippets.

@kmnk
Created July 20, 2017 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmnk/b88e4670bdedf7597fb12b4699f5657c to your computer and use it in GitHub Desktop.
Save kmnk/b88e4670bdedf7597fb12b4699f5657c to your computer and use it in GitHub Desktop.
train Zenject
// TapCounterInstaller.cs
using UnityEngine;
using System;
using UniRx;
using Zenject;
public class TapCounterInstaller : MonoInstaller<TapCounterInstaller>
{
public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<TapCounterModel>().AsSingle();
Container.BindInterfacesAndSelfTo<TapCounterPresenter>().AsSingle();
}
}
// 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()
{
}
}
// TapCounterView.cs
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using Zenject;
public class TapCounterView : MonoBehaviour
{
[SerializeField]
Button countButton;
[SerializeField]
Text countText;
public IObservable<Unit> CountButtonTapped { get; private set; }
public void Awake()
{
CountButtonTapped = countButton.OnClickAsObservable();
}
public void UpdateCount(int count)
{
countText.text = count.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment