简体   繁体   English

将两个不同对象的列表合并为一个。 C#Unity2D

[英]Merging two lists of different objects into one. C# Unity2D

First I want to give some context. 首先,我想提供一些背景信息。 I got two different classes (class Player and class Enemy), each class contains different data, but they both hold the value "int Initiative". 我得到了两个不同的类(Player类和Enemy类),每个类包含不同的数据,但是它们都具有“ int Initiative”值。

public class Enemy : MonoBehaviour 
{

    public int initiative;
    //More code

}
public class Player : MonoBehaviour 
{

    public int initiative;
    //More code
 }

On my project I have several units from both classes and they are stored on 2 different lists of objects 在我的项目中,我从这两个类都有几个单元,它们存储在2个不同的对象列表中

private List<Player> players;                           
private List<Enemy> enemies;

void Awake()
{
    players = new List<Player>();
    enemies = new List<Enemy>();
}

Is not shown in the code, but each unit is being sent and storaged on those list depending on their class. 没有显示在代码中,但是每个单元都根据其类别发送并存储在这些列表中。

Now my question: 现在我的问题是:

Is there any way of combining both list into a single list keeping all the different objects? 有什么方法可以将两个列表合并为一个包含所有不同对象的列表? (I tried to do this, but didn't get far) (我尝试这样做,但没有走远)

If there is no way of combining both lists because they contain different types of objects, could I create a single list that only storage the int initiative, type of object as well as the position on their previous list? 如果由于两个列表包含不同类型的对象而无法合并两个列表,我是否可以创建一个仅存储int主动性,对象类型以及它们在先前列表中的位置的列表? (so I can refer to the lists (players and enemies) when needed. Please explain me how I could achieve this (if possible with some code). (因此我可以在需要时参考列表(玩家和敌人)。请向我解释如何实现此目标(如果可能,可以使用一些代码)。

With this I am trying to create some sort of system that will look at the initiative of each unit and call them in order starting for the one that has the highest. 为此,我正在尝试创建一种系统,该系统将查看每个单元的主动性并调用它们,以便从具有最高性能的单元开始。

Please I am pretty new in C# and coding in general, so excuse me if the question is too simple, or doesn't make sense. 请问我在C#和编码方面还算是新手,所以如果问题太简单或没有道理,请原谅。

Thanks in advance :) 提前致谢 :)

You can do something like this: 您可以执行以下操作:

public class MonoBehaviour 
{
    public int initiative;//Since you are using inheritance this can be set here
}
public class Enemy : MonoBehaviour 
{
    //More code
}
public class Player : MonoBehaviour 
{
    //More code
}

Here is the code to merge them into one list: 以下是将它们合并到一个列表中的代码:

List<MonoBehaviour> list = new List<MonoBehaviour>()
list.Add(new Enemy());
list.Add(new Player());

When you want to process them differently somewhere for example you create a method as below: 例如,当您想在某处对它们进行不同的处理时,可以创建如下方法:

void ProcessList(List<MonoBehaviour> list)
{
    foreach(var l in list)
    {
        if(l is Enemy)
        {
            var enemy = (Enemy) l;
            //process the enemy
        }
        else
        {
            var player = (Player) l;
            //process as a player
        }
    }
}

You can use inheritance. 您可以使用继承。 Having base for both Enemy and Player . 具有EnemyPlayer

public class AliveEntity
{ 
    public string Name {get;set;}
    public double HP {get;set;}
}
public class Player : AliveEntity
{ /*...*/ }
public class Enemy : AliveEntity
{ /*...*/ }

And then the list could be List<AliveEntity> . 然后列表可以是List<AliveEntity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM