自学内容网 自学内容网

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;
         }
     }
 }

结果:

 


 


原文地址:https://blog.csdn.net/2401_82978699/article/details/143712851

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!