我正在学习 Unity 路径跟踪教程,我收到有关在 Int 类型上使用 .Select 的错误,但我不知道该行的作用

I'm following a Unity path tracing tutorial and I'm getting this error about using .Select on an Int type but I have no idea what the line does

也许在没有任何 C# 经验的情况下遵循 Unity 着色器教程并不是最好的主意。 :P

这是完整的错误:

'int[]' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'int[]' could be found (are you missing a using directive or an assembly reference?)

下面是该行所在的完整方法。如果有人能帮助我,我将不胜感激。

private void RebuildMeshObjectBuffers()
    {
        if(!_meshObjectsNeedRebuilding)
            return;
        
        _meshObjectsNeedRebuilding = false;
        _currentSample = 0;
        
        //Clear lists
        _meshObjects.Clear();
        _vertices.Clear();
        _indices.Clear();
        
        // Loop over all objects and gather their data
        foreach (RayTracingObject obj in _rayTracingObjects)
        {
            Mesh mesh = obj.GetComponent<MeshFilter>().sharedMesh;
            // Add vertex data
            int firstVertex = _vertices.Count;
            _vertices.AddRange(mesh.vertices);
            // Add index data - if the vertex buffer wasn't empty before, the
            // indices need to be offset
            int firstIndex = _indices.Count;
            var indices = mesh.GetIndices(0);
            _indices.AddRange(indices.Select(index => index + firstVertex));
            // Add the object itself
            _meshObjects.Add(new MeshObject()
            {
                localToWorldMatrix = obj.transform.localToWorldMatrix,
                indices_offset = firstIndex,
                indices_count = indices.Length
            });
        }
        
        CreateComputeBuffer(ref _meshObjectBuffer, _meshObjects, 72);
        CreateComputeBuffer(ref _vertexBuffer, _vertices, 12);
        CreateComputeBuffer(ref _indexBuffer, _indices, 4);
    }

您想使用 .Select 这是一个 linq 函数。

Mesh.GetIndices() returns .Select() 函数不可用的 int 数组。

你可以做的是先转换为列表:

            var indices = mesh.GetIndices(0);
            _indices.AddRange(indices.ToList().Select(index => index + firstVertex));

另见: https://docs.microsoft.com/en-us/dotnet/api/system.linq?view=net-5.0

编辑:

一些附加信息,您似乎不需要 ToList,您只是缺少导入 (using System.Linq)。 据我所知,.Select() 应该在 int[] 上可用,具体取决于您的框架版本;例如https://dotnetfiddle.net/oBPIbM