简体   繁体   English

如何指定多维数组的内容和 select 对象匹配列表中的条件?

[英]How can I specify the contents of a multidimensional array and select objects matching the criteria from a list?

Premise前提

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 次数。

  • Select the type of enemy to be generated from EnemyFactor Select 从EnemyFactor生成的敌人类型
  • Select the number of enemies to be generated from EnemyAmount Select 从EnemyAmount生成的敌人数量

在此处输入图像描述

What you want to achieve你想达到什么

  • Select enemy type from list and game object Select 列表和游戏中的敌人类型 object

Applicable source code适用的源代码

    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);
        }
    }

Problems问题

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的内容。

Supplementary information (FW / tool version, etc.)补充信息(固件/工具版本等)

  • Unity 2020.3.20f统一 2020.3.20f

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.

相关问题 如何搜索包含第一个条目的对象列表的对象列表,其中第一个条目包含与搜索条件匹配的元素? - How can I search a list of objects that contains a list of objects for the first entry that contains an element matching my search criteria? 选择返回真/假值的集合,而不是匹配条件的对象列表 - Select returning a collection of true/false values, not a list of objects matching criteria 从对象列表创建多维数组 - Creating a Multidimensional array from a list of objects 如何从具有数组条件的列表中选择 - how can I select from list with array condition 如何使用另一个列表中的数据更新列表的内容? - How can I update the contents of a list with data from another list? 如何将对象列表均匀分布到其他对象数组 - How can I evenly distribute a list of objects to an array of other objects 如何创建包含与条件匹配的索引的数组? - How do I create an array containing indexes matching a criteria? 从多维列表中选择数据 - Select data from Multidimensional List 如何实现 IEnumerable<t> 在具有多维数组内部列表的集合上?</t> - How can I implement IEnumerable<T> on a collection that has a MultiDimensional Array inner list? 如何在多维数组中选择逗号? - How to select the comma in multidimensional array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM