Created
July 20, 2017 04:33
-
-
Save kmnk/b88e4670bdedf7597fb12b4699f5657c to your computer and use it in GitHub Desktop.
train Zenject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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