UniRx对协程的支持
Coroutine转换为事件源
使用FromCoroutine可以把Coroutine转换为事件源(Observable)。
IEnumerator CoTest()
{
yield return new WaitForSeconds(1f);
Debug.Log("Test");
}
void Start()
{
Observable.FromCoroutine(CoTest)
.Subscribe(_ => {
//do something
})
.AddTo(this);
}
Observable转换为yield对象
使用ToYieldInstruction可以把Observable转换为yield对象。
IEnumerator CoTest()
{
yield return Observable.Timer(TimeSpan.FormSeconds(1f)).ToYieldInstruction();
Debug.Log("Test");
}
使用WhenAll并行操作Coroutine示例
IEnumerator A()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("A");
}
IEnumerator B()
{
yield return new WaitForSeconds(2.0f);
Debug.Log("B");
}
void Start()
{
var aStream = Observable.FromCoroutine(A());
var bStream = Observable.FromCoroutine(B());
Observable.WhenAll(aStream,bStream)
.Subscribe(_ =>
{
}).AddTo(this);
}
(END)