c# - 如何使用 SpawnCount 生成选定对象
问题描述
获取对象 && 获取其计数 SpawnCount
生成所有 20 个选定对象
遵循代码不只是示例。
using UnityEngine;
[System.Serializable]
public class ObjectToSpawn
{
public GameObject Object;
public int spawnCount;
}
public class LevelSpawner : MonoBehaviour
{
public ObjectToSpawn[] itemToSpawn; // Select Obstacles To spawn
public int maxSpawnObject; // Total Object or Sum of All Spawn Count.
}
解决方案
所以在澄清评论中的事情之后,我会说你只是想做
public class LevelSpawner : MonoBehaviour
{
public ObjectToSpawn[] itemToSpawn;
public int maxSpawnObject;
// Wherever this is called e.g. in "Awake"
public void Spawn()
{
// Keep track how many things you spawned in total
var counter = 0;
// iterate given items in order
foreach(var item in itemToSpawn)
{
// Iterate spawnCount times for the current item
for(var i = 0; i < item.spawnCount; i++)
{
// spawn the item
Instantiate(item.Object);
// increase the counter
counter++;
// return if max count is exceeded
if(counter >= maxSpawnObject) return;
}
}
}
}
推荐阅读
- python - 使用 f.read() 时,每个字母的迭代循环
- scikit-learn - 从分类数据集中删除冗余特征( make_classification )
- android - AppLifecycleState 何时切换 - 颤动
- php - 找不到语法错误的原因,文件意外结束
- php - Laravel 8 MVC 如何将值从控制器发送到视图
- excel - Excel 到同一个表的多个数据连接
- reactjs - 验证 Material UI TextField 提交
- c - 带浮点数的 scanf() 的最大字段宽度
- innerhtml - 样式不适用于离子“Angular”中的innerHTML
- here-api - 将 HERE 内容与其他提供商的地图图块混合