using System.Collections; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject prefab; // 需要生成的物体(Vehicle 或 Character) public Transform[] spawnPoints; // 生成点 public float spawnInterval = 3f; // 生成间隔 public int spawnCount = 1; public float destoryTime=5f; void Start() { StartCoroutine(SpawnCoroutine()); } private IEnumerator SpawnCoroutine() { while (true) { for (int i = 0; i < spawnCount; i++) { SpawnObject(); yield return new WaitForSeconds(Random.Range(0.5f, 1f)); // 生成间隔 } yield return new WaitForSeconds(spawnInterval); } } private IEnumerator DestoryObject(GameObject gameObject) { yield return new WaitForSeconds(destoryTime); Destroy(gameObject); } private void SpawnObject() { Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)]; GameObject obj = Instantiate(prefab, spawnPoint.position, Quaternion.identity,this.transform); StartCoroutine(DestoryObject(obj)); } }