「Unity/MVP/値変更を自動でViewへ適用」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「 ==MVPとは== MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン ==準備== # UniRxをインストールしておく。 Unity/...」) |
|||
| 行1: | 行1: | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==サンプル== | ==サンプル== | ||
| 行20: | 行12: | ||
set => counter.Value = value; | set => counter.Value = value; | ||
} | } | ||
| − | IntReactiveProperty counter = new IntReactiveProperty(); | + | public IntReactiveProperty counter = new IntReactiveProperty(); |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
public void PlusCounter() | public void PlusCounter() | ||
{ | { | ||
| 行78: | 行64: | ||
.Subscribe(_ => { | .Subscribe(_ => { | ||
_counterModel.PlusCounter(); | _counterModel.PlusCounter(); | ||
| − | |||
}) | }) | ||
.AddTo(this); | .AddTo(this); | ||
| 行86: | 行71: | ||
.Subscribe(_ => { | .Subscribe(_ => { | ||
_counterModel.MinusCounter(); | _counterModel.MinusCounter(); | ||
| − | |||
}) | }) | ||
.AddTo(this); | .AddTo(this); | ||
| 行94: | 行78: | ||
.Subscribe(_ => { | .Subscribe(_ => { | ||
_counterModel.SetCounter(_counterModel.Counter); | _counterModel.SetCounter(_counterModel.Counter); | ||
| − | |||
}) | }) | ||
.AddTo(this); | .AddTo(this); | ||
| + | // 値が変更したらView更新イベント発生 | ||
| + | _counterModel.counter.Subscribe(value => { | ||
| + | _counterView.SetValueText(_counterModel.Counter); | ||
| + | }); | ||
} | } | ||
void OnDestroy() | void OnDestroy() | ||
| 行112: | 行99: | ||
# CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | # CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | ||
# MainCanvasのCounterPresenterにCounterViewオブジェクトとCounterModelオブジェクトをドラッグする。 | # MainCanvasのCounterPresenterにCounterViewオブジェクトとCounterModelオブジェクトをドラッグする。 | ||
| − | |||
| − | |||
| − | |||
==参考== | ==参考== | ||
https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32 | https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32 | ||
2021年9月15日 (水) 07:51時点における版
サンプル
CounterModel.cs
using UnityEngine;
using UniRx;
public class CounterModel : MonoBehaviour
{
public int Counter {
get => counter.Value;
set => counter.Value = value;
}
public IntReactiveProperty counter = new IntReactiveProperty();
public void PlusCounter()
{
counter.Value++;
}
public void MinusCounter()
{
counter.Value--;
}
public void SetCounter(int value)
{
counter.Value = value;
}
}
CounterView.cs
using UnityEngine;
using UnityEngine.UI;
public class CounterView : MonoBehaviour
{
[SerializeField] public Button minusButton, plusButton, refreshButton;
[SerializeField] public Text text;
public void SetValueText(float value)
{
text.text = value.ToString();
}
}
CounterPresenter.cs
using UnityEngine;
using UniRx;
public class CounterPresenter : MonoBehaviour
{
[SerializeField] private CounterView _counterView;
[SerializeField] private CounterModel _counterModel;
void Start()
{
_counterView.SetValueText(_counterModel.Counter);
_counterView.plusButton
.OnClickAsObservable()
.Subscribe(_ => {
_counterModel.PlusCounter();
})
.AddTo(this);
_counterView.minusButton
.OnClickAsObservable()
.Subscribe(_ => {
_counterModel.MinusCounter();
})
.AddTo(this);
_counterView.refreshButton
.OnClickAsObservable()
.Subscribe(_ => {
_counterModel.SetCounter(_counterModel.Counter);
})
.AddTo(this);
// 値が変更したらView更新イベント発生
_counterModel.counter.Subscribe(value => {
_counterView.SetValueText(_counterModel.Counter);
});
}
void OnDestroy()
{
Destroy(this);
}
}
手順
- SampleSceneのヒエラルキーにTextとPlusButtonとMinusButtonとRefreshButtonを作成する。
- ヒエラルキーにGameObjectを作成して、CounterViewに名前を変更する。CounterView.csをAddComponentする。
- ヒエラルキーにGameObjectを作成して、CounterModelに名前を変更する。CounterModel.csをAddComponentする。
- CounterPresenter.csをMainCanvasにAddComponentする。
- CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。
- MainCanvasのCounterPresenterにCounterViewオブジェクトとCounterModelオブジェクトをドラッグする。
参考
https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32
