Files
aibis-dream/Assets/Scripts/AnimationManage/Spawner.cs
T
2025-05-29 15:27:58 +08:00

44 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));
}
}