Last active
October 3, 2019 08:48
-
-
Save arakaki-asdf/c541153f60129ec0fa7a051405748313 to your computer and use it in GitHub Desktop.
UniRx MVP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UniRx; | |
// Model データクラス | |
public class StatusModel | |
{ | |
// リアクティブプロパティ Subscribe可能 | |
public IntReactiveProperty RxHp = new IntReactiveProperty(); | |
public int Hp | |
{ | |
get { return RxHp.Value; } | |
set { RxHp.Value = value; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UniRx; | |
public class StatusPresenter : MonoBehaviour | |
{ | |
// view | |
[SerializeField] Slider slider; | |
[SerializeField] InputField inputField; | |
public StatusModel model = new StatusModel(); | |
void Start() | |
{ | |
// View -> Model イベント発火 | |
slider | |
.OnValueChangedAsObservable() | |
.Subscribe(x => | |
{ | |
model.Hp = (int)x; | |
Debug.Log("slider 発火"); | |
}) | |
.AddTo(this); | |
// View -> Model イベント発火 | |
inputField | |
.OnEndEditAsObservable() | |
.Select(v => int.Parse(v)) | |
.Select(v => Mathf.Clamp(v, 0, 300)) | |
.Subscribe(x => | |
{ | |
model.Hp = x; | |
Debug.Log("input field 発火"); | |
}) | |
.AddTo(this); | |
// Model -> View リアクティブ | |
model.RxHp | |
.AsObservable() | |
.Subscribe(x => | |
{ | |
slider.value = x; | |
inputField.text = x.ToString(); | |
Debug.Log("model リアクティブ"); | |
}) | |
.AddTo(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment