using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class generator : MonoBehaviour
{
public GameObject cylinderPrefab;//是公开的游戏对象字段,表示预设的圆柱体对象。
public Transform coordinate;//公开的变换对象,将用于设定新生成物体的位置。
public float interval = 1.0f;//控制生成圆柱体之间的间隔时间。
public Vector3 rotation;// 公开的旋转信息,设置新生成的圆柱体初始的朝向。
// Start is called before the first frame update
void Start()//开始函数,在游戏开始前被调用,这里启动了一个Coroutine (StartCoroutine(spawn())) 进行物体生成。
{
StartCoroutine(spawn());
}
// Update is called once per frame
IEnumerator spawn()//是一个异步迭代器,用于生成过程的控制流。它会持续运行,并在每次yield return后暂停。
{
while (true)//创建了一个无限循环,除非外部干预,否则不会停止。
{
GameObject spawnObject = Instantiate(cylinderPrefab, coordinate.position, Quaternion.identity);
//在当前位置和正方向(即四元数Quaternion.identity)创建一个新的圆柱体实例。
spawnObject.transform.eulerAngles = rotation;//设置新生成圆柱体的朝向为rotation。
yield return new WaitForSeconds(interval);// 暂停当前函数,等待指定的秒数,然后继续下一轮循环。
}
}
}
UNITY_生成器代码
最新推荐文章于 2024-10-16 15:24:54 发布