每隔几秒刷新一次
下面即为一个简单的定时器代码,可以实现每隔几秒就重复执行一段代码的功能,比较实用的代码段:
function Start () {
StartCoroutine("DoSomething");
}
function DoSomething () {
while (true) {
//需要重复执行的代码就放于在此处
print("DoSomething Loop");
//设置间隔时间为10秒
yield WaitForSeconds (10);
}
}
项目运用:
// Use this for initialization
void Start () {
StartCoroutine(UpdateRealTimeData());
}
// Update is called once per frame
void Update () {
}
IEnumerator UpdateRealTimeData()
{
while(true)
{
yield return new WaitForSeconds(5f);
foreach (var item in RealTimeDataButtonText)
{
var itemValue= Random.Range(MinValue-20, MaxValue+20);
if (itemValue < MinValue)
{
item.text = "<color=red>" + itemValue + "</color>";
}
else if (itemValue > MinValue && itemValue < MaxValue)
{
item.text = "<color=green>" + itemValue + "</color>";
}
else if (itemValue >MaxValue)
{
item.text = "<color=blue>" + itemValue + "</color>";
}
}
}
}

浙公网安备 33010602011771号