[英]How can I specify the contents of a multidimensional array and select objects matching the criteria from a list?
We are creating a tower defense type game.我们正在创建一款塔防类型的游戏。 So we want to build a Wave system and be able to set the number of Wave times each enemy will spring up according to their respective types.所以我们想建立一个 Wave 系统,并且能够根据他们各自的类型来设置每个敌人将 spring 起来的 Wave 次数。
EnemyFactor
Select 从EnemyFactor
生成的敌人类型EnemyAmount
Select 从EnemyAmount
生成的敌人数量 public class WaveListFactor : MonoBehaviour
{
public List<EnemyEnum> enemyFactor = new List<EnemyEnum>();
public List<int> enemyAmount = new List<int>();
}
public class WaveList : MonoBehaviour
{
public List<GameObject> enemyList = new List<GameObject>();
public List<WaveListFactor> waveEnemies = new List<WaveListFactor>();
}
public class WaveManager : MonoBehaviour
{
public void EnemySpawn()
{
int enemyFactor = waveList.waveEnemies.waveEnemyFactor;
GameObject enemy = waveList.enemyList[enemyFactor];
waveSpawner.Spawn(enemy);
}
}
int enemyFactor = waveList.waveEnemies.waveEnemyFactor;
Now I can't change EnemyEnum
in EnemyFactor
to int
and can't specify the contents of enemyList
.现在我无法将EnemyEnum
中的EnemyFactor
更改为int
并且无法指定enemyList
的内容。
I would recommend you rather use a SerializedDictionary<EnemyEnum, GameObject[]>
and simply configure beforehand which enum value results in which GameObject
prefab options (so you can still randomize a bit)我建议你宁愿使用SerializedDictionary<EnemyEnum, GameObject[]>
并简单地预先配置哪个枚举值导致哪个GameObject
预制选项(这样你仍然可以随机化一点)
public SerializedDictionary<EnemyEnum, GameObject[]> enemyList = new SerializedDictionary<EnemyEnum, GameObject[]>();
and then later select one doing然后 select 一个在做
var options = enemyList[enemyFactor];
and then select a random one like eg然后是 select 一个随机的,例如
var selectedPrefab = options[Random.Range(0, options.Length)];
Otherwise you would need some dedicated component on your enemies that provides the information about which EnemyEnum
it responds to like eg否则,您将需要一些专门用于敌人的组件,以提供有关它响应哪个EnemyEnum
的信息,例如
public class Enemy : MonoBehaviour
{
public EnymeEnum Type;
}
and rather have而有
public List<Enemy> enemyList = new List<Enemy>();
then you could filter your available options using Linq
然后您可以使用Linq
过滤可用选项
using System.Linq;
...
var options = enemyList.Where(enemy => enemy.Type == enemyFactor).ToArray();
and pick a random one from the existing matches like并从现有的比赛中随机挑选一个,比如
var selectedPrefab = options[Random.Range(0, options.Length)];
if this results in null
it simply means there is no matching prefab.如果结果为null
,则意味着没有匹配的预制件。
Disadvantage : Each prefab can only belong to one single enum category.缺点:每个预制件只能属于一个单一的枚举类别。 That's not the case for the other two solutions.其他两种解决方案并非如此。
Or even simplier: Why store the information in separate collections at all?甚至更简单:为什么要将信息存储在单独的 collections 中?
Simply rather have a只是有一个
[Seriaizable]
public class EnemyInfo
{
public GameObject Prefab;
public int Amount;
}
[Seriaizable]
public class WaveInfo
{
public EnemyInfo[] EnemyInfos;
}
this way you don't have to care about any enum but rather simply define how many instances of which prefab will be spawned.这样您就不必关心任何枚举,而只需定义将产生多少个预制件的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.