c# - 比较 gameObject 的变换并移除重复
问题描述
我有一个List<GameObject>
并且我需要删除具有相同position.y
和position.x
(position.z
无关紧要) 的双打。
我在想我需要比较列表中的所有元素并遍历同一列表的先前元素并比较它们是否具有相同的位置,如果没有,则填充new List<GameObject>()
.
我真的不知道该怎么做,因为我不是经验丰富的程序员。
解决方案
下面的功能应该可以正常工作
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;
}
推荐阅读
- html - 我需要制作一个 4 列 3 行的图片库,所有这些图片的大小都相同
- maven - 如何为 gradle 安装 sdkman
- javascript - 还有另一种在 URL http Delete 请求上添加 ID 的方法吗?
- javascript - Apollo Server gql 使用推荐
- python - 带有 dask sel 的 xarray 很慢
- python - Arduino到Python的Unicode转换为整数的问题
- c - 我无法从链接列表中删除所有节点
- javascript - Javascript - 使用户能够使用键盘上的指定键点亮页面上的框/项目
- python - 导入tensorflow导致python抛出错误并运行另一个脚本
- java - 连接表的一些优化