首页 > 解决方案 > 比较 gameObject 的变换并移除重复

问题描述

我有一个List<GameObject>并且我需要删除具有相同position.yposition.x(position.z无关紧要) 的双打。

我在想我需要比较列表中的所有元素并遍历同一列表的先前元素并比较它们是否具有相同的位置,如果没有,则填充new List<GameObject>().

我真的不知道该怎么做,因为我不是经验丰富的程序员。

标签: c#for-loopunity3d

解决方案


下面的功能应该可以正常工作

public static List<GameObject> Delete_Doubles(List<GameObject> gameobjects){
                    List<Vector2> occupied_coord = new List<Vector2>();
                    List<GameObject> gameobjects_after = new List<GameObjects>();
                    Vector2 t_vector = new Vector2();
                    Vector3 pos = new Vector3();
                    
                    pos = gameobjects[0].Transform.position;
                    t_vector.x = pos.x;
                    t_vector.y = pos.y;
                    gameobjects_after.Add(gameobjects[0]);
                    occupied_coord.Add(t_vector);
                    
                    for(int i = 1; i < gameobjects.Count; i++)
                    {
                        pos = gameobjects[i].Transform.position;
                        t_vector.x = pos.x;
                        t_vector.y = pos.y;
                        if (!occupied_coord.Contains(t_vector))
                        {
                             gameobjects_after.Add(gameobjects[i]);
                             occupied_coord.Add(t_vector);
                        }
                    }
                    return gameobjects_after;
        }

推荐阅读