CS0161 C#'':并非所有代码路径都返回值

问题描述

我收到此错误: CS0161 C#'':并非所有代码路径都返回值。 (如果可以帮助您,我正在使用以下视频:https://www.youtube.com/watch?v=QN39W020LqU&list=PLFt_AvWsXl0cONs3T0By4puYy6GM22ko8https://www.youtube.com/watch?v=wbpMiKiSKm8&list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3,我正在尝试使用陆地颜色代替行星中的渐变色) 这是代码

public Texture2D TextureFromHeightMap(float[,] heightMap)
{
    int width = heightMap.GetLength(0);
    int height = heightMap.GetLength(1);
    Texture2D texture = new Texture2D(width,height);

    Color[] colourMap = new Color[shapeSettings.planeTradius * shapeSettings.planeTradius];
    for (int y = 0; y < minMax.Max; y++)
    {
        for (int x = 0; x < shapeSettings.planeTradius; x++)
        {
            for (int i = 0; i < y; i++)
            {
                colourMap[y * width + x] = Color.Lerp(Color.black,Color.white,heightMap[x,y]);
            }
        }
        texture.SetPixels(colourMap);
        texture.Apply();
    }
}

解决方法

您必须返回纹理。

public Texture2D TextureFromHeightMap(float[,] heightMap)
{
    int width = heightMap.GetLength(0);
    int height = heightMap.GetLength(1);
    Texture2D texture = new Texture2D(width,height);

    Color[] colourMap = new Color[shapeSettings.planetRadius * shapeSettings.planetRadius];
    for (int y = 0; y < minMax.Max; y++)
    {
        for (int x = 0; x < shapeSettings.planetRadius; x++)
        {
            for (int i = 0; i < y; i++)
            {
                colourMap[y * width + x] = Color.Lerp(Color.black,Color.white,heightMap[x,y]);
            }
        }
        texture.SetPixels(colourMap);
        texture.Apply();
    }
    return texture;
}