unity3d————协程练习题
1.计秒器:
void Start()
{
StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
int time = 0;
while(true)
{
print(time + "秒");
time++;
yield return new WaitForSeconds(1);
}
}
结果:
2.生成多个cude (不卡顿):
void Start()
{
StartCoroutine(MyCoroutine(100000));
}
IEnumerator MyCoroutine(int num)
{
for(int i = 0; i < num; i++)
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
if(i % 100 == 0 && i != 0)
{
yield return null;
}
}
}
结果: