겨울팥죽 여름빙수

2023.02.24 - [게임을 만들자/Unity] - 1. UniRx 시작, Subject와 ReactiveProperty

2023.03.22 - [게임을 만들자/Unity] - 2. UniRx, Observable 메소드

2023.08.14 - [게임을 만들자/Unity] - 3. UniRx, 메시지 제어(Select, Where..)

 

 

  • Merge
    • 같은 타입의 메시지 스트림을 병합한다.
    • 병합되는 메시지 스트림 중 하나라도 메시지를 보내는 경우, 구독 함수가 실행된다.
    ReactiveProperty<int> a = new ();
    ReactiveProperty<int> b = new ();
    ReactiveProperty<int> c = new ();
    ReactiveProperty<int> d = new ();
    
    a.Merge(b, c, d)
    .ThrottleFrame(1) //ReactiveProperty라 구독과 동시에 4번 발생하므로, 마지막 값만 받도록
    .Subscribe(_ =>
    {
    		Debug.Log($"{_}");
    }).AddTo(this);
    
    b.Value = 1; //구독 함수에 1이 전달됨.
    c.Value = 3;
    a.Value = 5;
    
    ===============================================================
    0 //ReactiveProperty라 구독과 동시에 전달됨(d의 값).
    1
    3
    5
    

 

  •  CombineLastest
    • 메시지 스트림을 병합한다. 서로 다른 메시지 타입도 가능.
    • 맨 처음에는, 병합되는 스트림들 모두 메세지 발행이 되면, 병합한 메시지를 전달하고, 그 다음부터는, 스트림들 중 하나라도 메시지를 발행하면, 마지막으로 발행한 메시지들을 병합해 전달한다.
    ReactiveProperty<int> a = new (1);
    ReactiveProperty<string> b = new ("b");
    
    a.CombineLatest(b, (_a, _b) => (_a, _b))
    .Subscribe(_ =>
    {
    		var (a,b) = _;
    		Debug.Log($"{a}, {b}");
    }).AddTo(this);
    
    a.Value = 5;
    b.Value = "a"; //구독 함수에 1이 전달됨.
    
    ===============================================================
    1,b //ReactiveProperty라 구독과 동시에 전달됨
    5,b
    5,a
    
    • 만약 같은 타입끼리 CombineLastest를 한다면, 전달 값은 배열 타입으로 전달된다.
    ReactiveProperty<int> a = new ();
    ReactiveProperty<int> b = new ();
    ReactiveProperty<int> c = new ();
    ReactiveProperty<int> d = new ();
    
    a.CombineLatest(b, c, d, (_a,_b,_c,_d) => (_a,_b,_c,_d))
    .Subscribe(_ =>
    {
    		Debug.Log($"{_}");
    }).AddTo(this);
    
    b.Value = 1; //구독 함수에 1이 전달됨.
    c.Value = 3;
    a.Value = 5;
    b.Value = 1; //기존값과 같아서, 메시지 발행이 안된다.
    b.Value = 2;
    
    ===============================================================
    0,0,0,0 //ReactiveProperty라 구독과 동시에 전달됨
    0,1,0,0
    0,1,3,0
    5,1,3,0
    5,2,3,0
    
profile

겨울팥죽 여름빙수

@여름빙수

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!