Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Linq-C#Unity3D从何处获取索引数组_C#_Unity3d - Fatal编程技术网

使用Linq-C#Unity3D从何处获取索引数组

使用Linq-C#Unity3D从何处获取索引数组,c#,unity3d,C#,Unity3d,我有一组来自多边形碰撞器的矢量3。我想得到一个所有向量3的索引数组,这些索引高于某个y private void Awake() { polygonCollider2D = GetComponent<PolygonCollider2D>(); float lowestY = polygonCollider2D.points.Min(x => x.y); // So in the sentence below I want to

我有一组来自多边形碰撞器的矢量3。我想得到一个所有向量3的索引数组,这些索引高于某个y

    private void Awake()
    {
        polygonCollider2D = GetComponent<PolygonCollider2D>();
        float lowestY = polygonCollider2D.points.Min(x => x.y);
// So in the sentence below I want  to have a array of indexes instead of a array of vector3's.
        topPoints = polygonCollider2D.points.Where(x => x.y > lowestY).ToArray();
    }
private void Awake()
{
PolygonCallider2D=GetComponent();
float lowestY=polygonCallider2D.points.Min(x=>x.y);
//所以在下面的句子中,我想要一个索引数组,而不是向量数组。
topPoints=polygonCallider2D.points.Where(x=>x.y>lowestY).ToArray();
}

我可以用Linq做这个吗?

选择
可以捕获索引

您可以首先选择索引并使用下面使用的
-1
'filter sentinel'值进行筛选

topPointsIndexes = polygonCollider2D.points
    .Select((x, index) => (x.y > lowestY) ? index : -1) 
    .Where(i => i >= 0)
    .ToArray();

(或者首先将点和索引一起展开,然后过滤,就像juharr在回答中所做的那样)

是的,您可以使用包含索引的重载,如下所示

var topPointIndexes = polygonCollider2D.points
    .Select((p,i) => new{Point=p, Index=i})
    .Where(x => x.Point.y > lowestY)
    .Select(x => x.Index)
    .ToArray();
另一种选择是只提前创建一组索引

var points = polygonCollider2D.points;
var topPointIndexes = Enumerable.Range(0, points.Count())
    .Where(i => points[i].y > lowestY)
    .ToArray();
试试看


看看包含索引的重载。
indexesOfTopPoints = polygonCollider2D.points.Select((x, index) => index)
.Where(x => polygonCollider2D.points[x].y > lowestY).ToArray();
topPoints = polygonCollider2D.points.Select((x, i) => new {obj = x, i = i}).Where(x => x.y > lowestY).Select(x => x.i).ToArray();