当鼠标按下时,do something
void Start()
{
Observable.EveryUpdate() //开启update事件监听
.Where(_ => Input.GetMouseButtonDown(0)) //进行一个判断
.First() //只获取第一次的点击事件
.Subscribe(_ => //订阅(处理)一个事件
{
Debug.Log("down!");
}
);
}
定时功能,2s之后开始做某事
void Start()
{
Observable.Timer(TimeSpan.FromSeconds(2.0f))
.Subscribe(_ =>
{
Debug.Log("do something");
});
}
一个字母一个字母的列印
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityRx;
using UnityRx.Triggers;
public class test : MonoBehaviour
{
void Start()
{
var urls = new List<string> { "abcdefg", "gfedcba" };
urls.SelectMany(url => url)
.ToList()
.ForEach(singlechar =>
{
Debug.Log(singlechar);
});
}
}
take 执行前几次(First 只执行一次)
void Start()
{
var urls = new string[] {
"http://sikiedu.com",
"http://qframeowork.io",
"http://weibo.com",
"http://github.com",
};
urls.Take(3)
.ToList()
.ForEach(url =>
{
Debug.Log(url);
});
}
结果列印出
"http://sikiedu.com",
"http://qframeowork.io",
"http://weibo.com",