unity计时,1秒执行一次

1.核心代码示例

unity计时,1秒执行一次_第1张图片

 2.代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddRandomChilden : MonoBehaviour
{
    // Debug.Log("开始进入update");
    float i = 0;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (i >1)
        {
            Debug.Log("开始创建sphere");;
            int xRange=Random.Range(-10,10);
            int zRange=Random.Range(-10,10);
            GameObject objB = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere), transform);
            objB.transform.position = new Vector3(xRange, 0,zRange);
            objB.transform.localScale = new Vector3(0.3f, 0.3f,0.3f);
            i = 0;
        }
        i += Time.deltaTime ;
        Debug.Log(i);
    }
}

你可能感兴趣的:(unity,游戏引擎)