「Unity/MVP」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→参考) |
|||
| 行10: | 行10: | ||
CounterModel.cs | CounterModel.cs | ||
<pre> | <pre> | ||
| − | public class CounterModel | + | using UnityEngine; |
| + | using UniRx; | ||
| + | |||
| + | public class CounterModel : MonoBehaviour | ||
{ | { | ||
public int Counter { | public int Counter { | ||
| − | get => counter; | + | get => counter.Value; |
| − | set => counter = value; | + | set => counter.Value = value; |
} | } | ||
| − | + | IntReactiveProperty counter = new IntReactiveProperty(); | |
| + | void Start() | ||
| + | { | ||
| + | counter.Subscribe(value => { | ||
| + | Debug.Log("change value=" + value); | ||
| + | }); | ||
| + | } | ||
public void PlusCounter() | public void PlusCounter() | ||
{ | { | ||
| − | counter++; | + | counter.Value++; |
} | } | ||
public void MinusCounter() | public void MinusCounter() | ||
{ | { | ||
| − | counter--; | + | counter.Value--; |
| + | } | ||
| + | public void SetCounter(int value) | ||
| + | { | ||
| + | counter.Value = value; | ||
} | } | ||
} | } | ||
| 行36: | 行49: | ||
public class CounterView : MonoBehaviour | public class CounterView : MonoBehaviour | ||
{ | { | ||
| − | [SerializeField] public Button minusButton, plusButton; | + | [SerializeField] public Button minusButton, plusButton, refreshButton; |
[SerializeField] public Text text; | [SerializeField] public Text text; | ||
| 行54: | 行67: | ||
{ | { | ||
[SerializeField] private CounterView _counterView; | [SerializeField] private CounterView _counterView; | ||
| − | private CounterModel _counterModel; | + | [SerializeField] private CounterModel _counterModel; |
void Start() | void Start() | ||
{ | { | ||
| − | |||
_counterView.SetValueText(_counterModel.Counter); | _counterView.SetValueText(_counterModel.Counter); | ||
| 行73: | 行85: | ||
.Subscribe(_ => { | .Subscribe(_ => { | ||
_counterModel.MinusCounter(); | _counterModel.MinusCounter(); | ||
| + | _counterView.SetValueText(_counterModel.Counter); | ||
| + | }) | ||
| + | .AddTo(this); | ||
| + | |||
| + | _counterView.refreshButton | ||
| + | .OnClickAsObservable() | ||
| + | .Subscribe(_ => { | ||
| + | _counterModel.SetCounter(_counterModel.Counter); | ||
_counterView.SetValueText(_counterModel.Counter); | _counterView.SetValueText(_counterModel.Counter); | ||
}) | }) | ||
| 行85: | 行105: | ||
==手順== | ==手順== | ||
| − | # | + | # SampleSceneのヒエラルキーにTextとPlusButtonとMinusButtonとRefreshButtonを作成する。 |
| − | # | + | # ヒエラルキーにGameObjectを作成して、CounterViewに名前を変更する。CounterView.csをAddComponentする。 |
| + | # ヒエラルキーにGameObjectを作成して、CounterModelに名前を変更する。CounterModel.csをAddComponentする。 | ||
# CounterPresenter.csをMainCanvasにAddComponentする。 | # CounterPresenter.csをMainCanvasにAddComponentする。 | ||
# CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | # CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | ||
| − | # | + | # MainCanvasのCounterPresenterにCounterViewオブジェクトとCounterModelオブジェクトをドラッグする。 |
==参考== | ==参考== | ||
2021年9月15日 (水) 05:53時点における版
MVPとは
MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン
準備
- UniRxをインストールしておく。
Unity/UniRx [ショートカット]
サンプル
CounterModel.cs
using UnityEngine;
using UniRx;
public class CounterModel : MonoBehaviour
{
public int Counter {
get => counter.Value;
set => counter.Value = value;
}
IntReactiveProperty counter = new IntReactiveProperty();
void Start()
{
counter.Subscribe(value => {
Debug.Log("change value=" + value);
});
}
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();
_counterView.SetValueText(_counterModel.Counter);
})
.AddTo(this);
_counterView.minusButton
.OnClickAsObservable()
.Subscribe(_ => {
_counterModel.MinusCounter();
_counterView.SetValueText(_counterModel.Counter);
})
.AddTo(this);
_counterView.refreshButton
.OnClickAsObservable()
.Subscribe(_ => {
_counterModel.SetCounter(_counterModel.Counter);
_counterView.SetValueText(_counterModel.Counter);
})
.AddTo(this);
}
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/OKsaiyowa/items/745c5359682c7baad6bf
https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32
