1、每帧检查(Time.deltaTime)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour
{
public float m_Time = 1f;
void Update()
{
m_Time -= Time.deltaTime;
if (m_Time <= 0)
{
//每隔一秒改变一次颜色
ChangeColor(gameObject);
m_Time = 1f;
}
}
public void ChangeColor(GameObject go)
{
go.GetComponent<MeshRenderer>().material.color = new Color(Random.Range(0, 255) / 255f,
Random.Range(0, 255) / 255f, Random.Range(0, 255) / 255f, 1f);
}
}
2、使用协程(IEnumerator)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Timer : MonoBehaviour
{
void Start()
{
StartCoroutine(ChangeColor());
}
IEnumerator ChangeColor()
{
while (true)// 还需另