びぼうろくってみんなやってる

みんなやってるからぼくもやる

UniRx ReactiveProperty めも

  • 下記のようなやつを
public interface IReactiveTestModel
{
  IReadOnlyReactiveProperty<int> Num { get; }
  IReadOnlyReactiveProperty<string> Text { get; }

  void UpdateProperties(int updateNum, string updateText)
}

public class ReactiveTestModel : IReactiveTestModel
{
  public IReadOnlyReactiveProperty<int> Num { get {return num;} }
  public IReadOnlyReactiveProperty<string> Text { get {return text;} }
  
  IntReactiveProperty num = new IntReactiveProperty();
  StringReactiveProperty text = new StringReactiveProperty();

  public ReactiveTest(int initNum, string initText)
  {
    num.Value = initNum;
    text.Value = initText;
  }

  public void UpdateProperties(int updateNum, int updateText)
  {
    num.Value = updateNum;
    text.Value = updateText;
  }
}
  • 下記のように使う
public class ReactiveTest : MonoBehaviour
{
  [SerializeField] Text level;
  [SerializeField] Text description;

  IReactiveTestModel model;
  void Init(IReactiveTestModel model)
  {
    this.model = model;
    
    model.num.Subscribe(n => {level.text = n.ToString(); });
    model.text.Subscribe(t => { description.text = t; });
  }

  void SetModelValues(int currentLevel, string currentDescription)
  {
    model.UpdateProperties(currentLevel, currentDescription);
  }
}
  • Modelの数値の変化を監視して、それを反映させることができる。
  • 他にもReactiveCollectionとかいろいろある。