Open3

UniRx メモ

cgccgc

lazy な IObservable (アクセスした際に、Stream元のStartが呼び出されていないケースなどを想定)

ソース
  • Stream元

public class SpotManager : MonoBehaviour
{
    [SerializeField]
    public List<Spot> spots = new();

    private IObservable<bool> hasEmptyObjectStream;
    public IObservable<bool> HasEmptyObjectStream
    {
        get
        {
            if (hasEmptyObjectStream == null)
            {
                hasEmptyObjectStream = spots
                    .Select(sp => sp.isEmptyObject)
                    .CombineLatest()
                    .Select(isEmpty => isEmpty.All(b => !b))
                    .Replay(1) // Cache
                    .RefCount(); // 
            }
            return hasEmptyObjectStream;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        SetSpot(spots);
    }

    public void SetSpot(List<Spot> newSpots)
    {
        spots = newSpots;
        hasEmptyObjectStream = null;
        var lazy = HasEmptyObjectStream; //初期化

    }
}
  • 購読側
public class CanvasUIManager : MonoBehaviour
{
    [SerializeField] private Button buttonSubmit;
    [SerializeField] private SpotManager spotManager;

    // Start is called before the first frame update
    void Start()
    {
        spotManager.HasEmptyObjectStream
            .Subscribe(hasEmpty => buttonSubmit.interactable = hasEmpty);

    }

    // Update is called once per frame
    void Update()
    {

    }
}
cgccgc

BehaviorSubject VS ReactiveProperty

  • シリアライズが必要かどうか
  • ストリーム操作が要求されるかどうか
  • バインディングシステムやインスペクターと連携させる必要があるかどうか