카테고리 없음

[Unity] unirx 더블클릭 판정 예제

Kim2558 2018. 1. 25. 03:35

더블클릭 판정 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using UniRx.Triggers;
using System;
 
public class DoubleClick : MonoBehaviour {
 
    // Use this for initialization
    void Start () {
 
        var clickStream = this.UpdateAsObservable().Where(_ => Input.GetMouseButtonDown(0));
 
        clickStream
            .Buffer(clickStream.Throttle(TimeSpan.FromMilliseconds(200)))
            .Where(x => x.Count >= 2)
            .Subscribe(_ => Debug.Log("DoubleClick"));           
    }
}
 
cs


clickStream 은 매 Update 시 마우스 왼쪽 클릭이 있을 때 발생한다. 발생한 clickStream은 Buffer 에 저장된다. Buffer의 개방 조건은 throttle 이다.  위 코드의 throttle은 clickStream 이 발생한 뒤 0.2초 동안 새로운 clickStream이 발생하지 않으면 발생한다. 즉 첫 번째 클릭 후 0.2 초간 다른 클릭이 없었다면 throttle 이 발생되고 buffer 의 개수는 한 개 이므로 Where 절을 통과하지 못한다. 만약 첫 번째 클릭 이후 0.2초 안에 새로운 clickStream 이 발생했다면 buffer는 2 이상의 값을 가져 where 절을 통과하고 subscribe 된다.


실행 영상