2017년 8월 7일 월요일

UniRx Note3- OnTriggerXXXXAsObservable (extension method)

void Test_OnTrigger()
{
    Rigidbody rigidBody = GetComponent<Rigidbody>();

    // OnTrigger
    Debug.Log("---------------------------------------------------------------------------------------------------");
    Debug.Log("OnTrigger Enter/Stay/Exit AsObservable Test");

    bool zoneFlag = false;

    // 플래그가 유효한 동안 위쪽으로 힘을 가하면 
    this.FixedUpdateAsObservable()
        .Where(_ => zoneFlag)
        .Subscribe(_ => rigidBody.AddForce(Vector3.up));

    // Zone에 들어오면 하면 플래그를 활성화
    this.OnTriggerEnterAsObservable()
        .Where(x => x.gameObject.tag == "Zone")
        .Subscribe(_ => { 
            zoneFlag = true;
            Debug.Log("Zone Enter");
        });

    this.OnTriggerStayAsObservable()
        .Where(x => x.gameObject.tag == "Zone")
        .Subscribe(_ => { 
            Debug.Log("Zone Stay");
        });

    // Zone에서 나가면 플래그를 해제 
    this.OnTriggerExitAsObservable()
        .Where(x => x.gameObject.tag == "Zone")
        .Subscribe(_ => { 
            zoneFlag = false;
            Debug.Log("Zone Exit");
        });
}

댓글 없음:

댓글 쓰기