「Unity/MVP」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==MVPとは== MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン」) |
|||
行1: | 行1: | ||
==MVPとは== | ==MVPとは== | ||
MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン | MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン | ||
+ | |||
+ | CounterModel.cs | ||
+ | <pre> | ||
+ | public class CounterModel | ||
+ | { | ||
+ | public int Counter { | ||
+ | get => counter; | ||
+ | set => counter = value; | ||
+ | } | ||
+ | int counter = 0; | ||
+ | |||
+ | public void PlusCounter() | ||
+ | { | ||
+ | counter++; | ||
+ | } | ||
+ | public void MinusCounter() | ||
+ | { | ||
+ | counter--; | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | CounterView.cs | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UnityEngine.UI; | ||
+ | |||
+ | public class CounterView : MonoBehaviour | ||
+ | { | ||
+ | [SerializeField] public Button minusButton, plusButton; | ||
+ | [SerializeField] public Text text; | ||
+ | |||
+ | public void SetValueText(float value) | ||
+ | { | ||
+ | text.text = value.ToString(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | CounterPresenter.cs | ||
+ | <pre> | ||
+ | using UnityEngine; | ||
+ | using UniRx; | ||
+ | |||
+ | public class CounterPresenter : MonoBehaviour | ||
+ | { | ||
+ | [SerializeField] private CounterView _counterView; | ||
+ | |||
+ | void Start() | ||
+ | { | ||
+ | CounterModel model = new CounterModel(); | ||
+ | _counterView.SetValueText(model.Counter); | ||
+ | |||
+ | _counterView.plusButton | ||
+ | .OnClickAsObservable() | ||
+ | .Subscribe(_ => { | ||
+ | model.PlusCounter(); | ||
+ | _counterView.SetValueText(model.Counter); | ||
+ | }) | ||
+ | .AddTo(this); | ||
+ | |||
+ | _counterView.minusButton | ||
+ | .OnClickAsObservable() | ||
+ | .Subscribe(_ => { | ||
+ | model.MinusCounter(); | ||
+ | _counterView.SetValueText(model.Counter); | ||
+ | }) | ||
+ | .AddTo(this); | ||
+ | } | ||
+ | } | ||
+ | </pre> |
2021年9月15日 (水) 03:19時点における版
MVPとは
MV(R)Pで、ModelとViewとPresenterをReactiveで、疎結合に連携したパターン
CounterModel.cs
public class CounterModel { public int Counter { get => counter; set => counter = value; } int counter = 0; public void PlusCounter() { counter++; } public void MinusCounter() { counter--; } }
CounterView.cs
using UnityEngine; using UnityEngine.UI; public class CounterView : MonoBehaviour { [SerializeField] public Button minusButton, plusButton; [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; void Start() { CounterModel model = new CounterModel(); _counterView.SetValueText(model.Counter); _counterView.plusButton .OnClickAsObservable() .Subscribe(_ => { model.PlusCounter(); _counterView.SetValueText(model.Counter); }) .AddTo(this); _counterView.minusButton .OnClickAsObservable() .Subscribe(_ => { model.MinusCounter(); _counterView.SetValueText(model.Counter); }) .AddTo(this); } }