「Unity/MVP/値変更を自動でViewへ適用」の版間の差分
提供: 初心者エンジニアの簡易メモ
| (同じ利用者による、間の3版が非表示) | |||
| 行1: | 行1: | ||
| − | |||
==サンプル== | ==サンプル== | ||
CounterModel.cs | CounterModel.cs | ||
<pre> | <pre> | ||
| − | |||
using UniRx; | using UniRx; | ||
| − | public class CounterModel | + | public class CounterModel |
{ | { | ||
public int Counter { | public int Counter { | ||
| − | get => | + | get => counterRP.Value; |
| − | set => | + | set => counterRP.Value = value; |
} | } | ||
| − | public IntReactiveProperty | + | public IntReactiveProperty counterRP = new IntReactiveProperty(); |
public void PlusCounter() | public void PlusCounter() | ||
{ | { | ||
| − | + | counterRP.Value++; | |
} | } | ||
public void MinusCounter() | public void MinusCounter() | ||
{ | { | ||
| − | + | counterRP.Value--; | |
} | } | ||
public void SetCounter(int value) | public void SetCounter(int value) | ||
{ | { | ||
| − | + | counterRP.Value = value; | |
} | } | ||
} | } | ||
| 行54: | 行52: | ||
{ | { | ||
[SerializeField] private CounterView _counterView; | [SerializeField] private CounterView _counterView; | ||
| − | + | private CounterModel _counterModel; | |
void Start() | void Start() | ||
{ | { | ||
| + | _counterModel = new CounterModel(); | ||
_counterView.SetValueText(_counterModel.Counter); | _counterView.SetValueText(_counterModel.Counter); | ||
| + | Bind(); | ||
| + | } | ||
| + | private void Bind() | ||
| + | { | ||
_counterView.plusButton | _counterView.plusButton | ||
.OnClickAsObservable() | .OnClickAsObservable() | ||
| 行81: | 行84: | ||
.AddTo(this); | .AddTo(this); | ||
// 値が変更したらView更新イベント発生 | // 値が変更したらView更新イベント発生 | ||
| − | _counterModel. | + | _counterModel.counterRP.Subscribe(value => { |
_counterView.SetValueText(_counterModel.Counter); | _counterView.SetValueText(_counterModel.Counter); | ||
}); | }); | ||
| 行95: | 行98: | ||
# SampleSceneのヒエラルキーにTextとPlusButtonとMinusButtonとRefreshButtonを作成する。 | # SampleSceneのヒエラルキーにTextとPlusButtonとMinusButtonとRefreshButtonを作成する。 | ||
# ヒエラルキーにGameObjectを作成して、CounterViewに名前を変更する。CounterView.csをAddComponentする。 | # ヒエラルキーにGameObjectを作成して、CounterViewに名前を変更する。CounterView.csをAddComponentする。 | ||
| − | |||
# CounterPresenter.csをMainCanvasにAddComponentする。 | # CounterPresenter.csをMainCanvasにAddComponentする。 | ||
# CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | # CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。 | ||
| − | # | + | # MainCanvasのCounterPresenterにCounterViewオブジェクトをドラッグする。 |
==参考== | ==参考== | ||
https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32 | https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32 | ||
| + | |||
| + | https://developers.cyberagent.co.jp/blog/archives/4262/ | ||
2021年9月15日 (水) 17:18時点における最新版
サンプル
CounterModel.cs
using UniRx;
public class CounterModel
{
public int Counter {
get => counterRP.Value;
set => counterRP.Value = value;
}
public IntReactiveProperty counterRP = new IntReactiveProperty();
public void PlusCounter()
{
counterRP.Value++;
}
public void MinusCounter()
{
counterRP.Value--;
}
public void SetCounter(int value)
{
counterRP.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;
private CounterModel _counterModel;
void Start()
{
_counterModel = new CounterModel();
_counterView.SetValueText(_counterModel.Counter);
Bind();
}
private void Bind()
{
_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.counterRP.Subscribe(value => {
_counterView.SetValueText(_counterModel.Counter);
});
}
void OnDestroy()
{
Destroy(this);
}
}
手順
- SampleSceneのヒエラルキーにTextとPlusButtonとMinusButtonとRefreshButtonを作成する。
- ヒエラルキーにGameObjectを作成して、CounterViewに名前を変更する。CounterView.csをAddComponentする。
- CounterPresenter.csをMainCanvasにAddComponentする。
- CounterViewオブジェクトを選択し、インスペクターのCounterViewの部分にそれぞれのUIをドラッグする。
- MainCanvasのCounterPresenterにCounterViewオブジェクトをドラッグする。
参考
https://qiita.com/Nakashima_Toshiki/items/5e0c36c3b0df78110d32
