using UniRx;
using UnityEngine;
public class GameData : MonoBehaviour
{
//int 型の ReactiveProperty を作成
public ReactiveProperty<int> Count = new();
void Reset()
{
//ReactiveProperty に初期値を代入
Count = new(0);
}
}
GameManager.cs(Presenter)
~AsObservableメソッドなし
using UniRx;
using UnityEngine;
public class GameManager : MonoBehaviour
{
[SerializeField]
private UIManager uiManager;
[SerializeField]
private GameData gameData;
private void Start()
{
gameData.Count
.Subscribe(x => uiManager.UpdateDisplayCount(x))
.AddTo(this);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))gameData.Count.Value++;
}
}
~AsObservableメソッドあり
using UniRx;
using UniRx.Triggers;
using UnityEngine;
public class GameManager : MonoBehaviour
{
[SerializeField]
private UIManager uiManager;
[SerializeField]
private GameData gameData;
private void Start()
{
gameData.Count
.Subscribe(x => uiManager.UpdateDisplayCount(x))
.AddTo(this);
this.UpdateAsObservable()
.Where(_ => Input.GetMouseButtonDown(0))
.Subscribe(_ => gameData.Count.Value++)
.AddTo(this);
}
}
UIManager.cs(View)
using UnityEngine.UI;
using UnityEngine;
public class UIManager : MonoBehaviour
{
[SerializeField]
private Text txtCount;
public void UpdateDisplayCount(int count)//引数を利用
{
txtCount.text = count.ToString();
}
}