public interface ICounter
{
int GetCount();
void CounterStart();
void CounterReset();
}
CounterStorage.cs
using System;
using UniRx;
public class CounterStorage : ICounter
{
private int count = 0;
public int GetCount()
{
return count;
}
public void CounterStart()
{
Observable.Timer(TimeSpan.FromSeconds(1))
.Repeat()
.Subscribe(_ => count++);
}
public void CounterReset()
{
count = 0;
}
}
CounterInstaller.cs
using UnityEngine;
using Reflex.Core;
public class CounterInstaller : MonoBehaviour, IInstaller
{
public void InstallBindings(ContainerDescriptor descriptor)
{
descriptor.AddSingleton(typeof(CounterStorage), typeof(ICounter));
}
}
ViewCounter.cs
using Reflex.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ViewCounter : MonoBehaviour
{
[SerializeField] private Text text;
[SerializeField] private Button startbutton;
[SerializeField] private Button resetbutton;
[Inject] private ICounter counter;
private void Start()
{
startbutton.onClick.AddListener(CounterStart);
resetbutton.onClick.AddListener(Reset);
}
private void Update()
{
text.text = $"{counter.GetCount()}";
}
private void CounterStart()
{
counter.CounterStart();
}
private void Reset()
{
counter.CounterReset();
SceneManager.LoadScene("ReflexUniRx");
}
}