1.在指定位置生成物体,将生成的物体直接作为指定父级的子级
代码实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 挂载到父物体上 动态生成物体
/// </summary>
public class SpawnManager : MonoBehaviour
{
// 在Inspector中指定生成位置的参考对象
public Transform spawnReference;
// 在Inspector中指定生成的物体类型
public GameObject[] spawnPrefabs;
// 在Inspector中指定生成间隔时间
public float spawnInterval = 1.0f;
// 在Inspector中指定父级对象
public Transform parentTransform;
// 用于存储生成的物体,以便后续管理
//public GameObject[] spawnedObjects;
//private int currentIndex = 0;
// Start is called before the first frame update
void Start()
{
// 初始化数组,假设最多生成10个物体
//spawnedObjects = new GameObject[10];
// 开始生成物体
InvokeRepeating("SpawnObject", 0.0f, spawnInterval);
}
void SpawnObject()
{
如果数组已满,停止生成
//if (currentIndex >= spawnedObjects.Length)
//{
// Debug.Log("Reached maximum number of spawned objects.");
// ClearSpawnedObjects();
// return;
//}
// 随机选择一个预制体
int prefabIndex = Random.Range(0, spawnPrefabs.Length);
GameObject prefabToSpawn = spawnPrefabs[prefabIndex];
// 获取生成位置
Vector3 spawnPosition = spawnReference != null ? spawnReference.position : Vector3.zero;
// 实例化物体,并将其设置为指定父级的子级
GameObject spawnedObject = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity, parentTransform);
MoveObject moveobject = spawnedObject.GetComponent<MoveObject>();
if (moveobject != null)
{
// 成功获取脚本 设置存活时间
moveobject.SetLifetime(3f);
}
else
{
Debug.LogError("未找到目标脚本");
}
将生成的物体存储在数组中
//spawnedObjects[currentIndex] = spawnedObject;
增加索引
//currentIndex++;
}
提供一个方法来清理生成的物体
//public void ClearSpawnedObjects()
//{
// for (int i = 0; i < currentIndex; i++)
// {
// if (spawnedObjects[i] != null)
// {
// Destroy(spawnedObjects[i]);
// }
// }
// // 初始化数组,假设最多生成10个物体
// spawnedObjects = new GameObject[10];
// // 重置索引
// currentIndex = 0;
//}
}
2.在指定范围内,随机生成物体
代码实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 挂载到父物体上 动态随机位置生成物体
/// </summary>
public class RandomSpawner : MonoBehaviour
{
// 在Inspector中指定生成位置的参考对象
public Transform spawnReference;
// 在Inspector中指定生成的物体类型
public GameObject[] spawnPrefabs;
// 在Inspector中指定生成间隔时间
public float spawnInterval = 1.0f;
// 在Inspector中指定父级对象
public Transform parentTransform;
// 在Inspector中指定随机生成范围
public Vector2 spawnArea = new Vector2(10f, 10f);
void Start()
{
// 检查是否有可生成的预制体
if (spawnPrefabs == null || spawnPrefabs.Length == 0)
{
Debug.LogError("未指定生成的预制体!");
return;
}
// 开始生成物体
InvokeRepeating("SpawnObject", 0.0f, spawnInterval);
}
void SpawnObject()
{
// 随机选择一个预制体
int prefabIndex = Random.Range(0, spawnPrefabs.Length);
GameObject prefabToSpawn = spawnPrefabs[prefabIndex];
// 获取生成位置
Vector3 spawnPosition = GetRandomSpawnPosition();
// 实例化物体,并将其设置为指定父级的子级
GameObject spawnedObject = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity, parentTransform);
// 设置物体的存活时间 10秒
SetObjectLifetime(spawnedObject, 10f);
}
Vector3 GetRandomSpawnPosition()
{
// 如果指定了参考对象,则以参考对象的位置为中心生成
Vector3 center = spawnReference != null ? spawnReference.position : Vector3.zero;
// 在指定范围内随机生成位置
float randomX = Random.Range(-spawnArea.x / 2, spawnArea.x / 2);
float randomZ = Random.Range(-spawnArea.y / 2, spawnArea.y / 2);
return center + new Vector3(randomX, 0, randomZ);
}
void SetObjectLifetime(GameObject obj, float lifetime)
{
MoveObject moveObject = obj.GetComponent<MoveObject>();
if (moveObject != null)
{
// 成功获取脚本,设置存活时间
moveObject.SetLifetime(lifetime);
}
else
{
Debug.LogError("未找到目标脚本:MoveObject");
}
}
}