Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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
C# Unity3d方法必须具有返回类型和错误CS0501_C#_Unity3d - Fatal编程技术网

C# Unity3d方法必须具有返回类型和错误CS0501

C# Unity3d方法必须具有返回类型和错误CS0501,c#,unity3d,C#,Unity3d,我正在学习关于体素网格的教程,遇到了一个我没有预料到的错误。这是第二部分,如果你去看看的话。我还认为这可能是一个统一版本的问题,因为这是从2014年开始的。我搜索了错误,没有发现结论性结果。我一次又一次地检查无效陈述,看到许多重复的问题。我还多次尝试重新启动unity。关于错误的更具体信息:在第118119行和第120行,我得到了这两个错误。错误CS0501:方法必须具有返回类型。错误CS0501:“PolygongGenerator.PolygongGenerator()”必须声明一个主体,因

我正在学习关于体素网格的教程,遇到了一个我没有预料到的错误。这是第二部分,如果你去看看的话。我还认为这可能是一个统一版本的问题,因为这是从2014年开始的。我搜索了错误,没有发现结论性结果。我一次又一次地检查无效陈述,看到许多重复的问题。我还多次尝试重新启动unity。关于错误的更具体信息:在第118119行和第120行,我得到了这两个错误。错误CS0501:方法必须具有返回类型。错误CS0501:“PolygongGenerator.PolygongGenerator()”必须声明一个主体,因为它没有标记为抽象、外部或部分。无论如何,我的代码如下:(另外,请更正我的评论并添加更多)

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类多工发生器:单行为
{
//第一个列表包含要渲染的网格的每个顶点
public List new顶点=new List();
//三角形告诉Unity如何构建网格连接的每个部分
//顶点
public List newTriangles=新列表();
//UV列表现在并不重要,但它告诉Unity纹理是如何的
//在每个多边形上对齐
public List newUV=新列表();
专用浮点数t=0.25f;
私有向量2 tStone=新向量2(0,0);
私有向量2 tGrass=新向量2(0,1);
//网格由我们要定义的顶点、三角形和UV组成,
//完成后,我们将它们保存为此网格
私有网格;
私有整数平方计数;
公共字节[,]块;//0=air 1=rock,2=grass
//在第一帧更新之前调用Start
void Start()
{
//获取游戏对象的网格
mesh=GetComponent().mesh;
//获取游戏对象的x、y和z值
//写x比变换.position.x容易很多次
float x=transform.position.x;
float y=transform.position.y;
float z=transform.position.z;
//定义网格的哪些角用于纹理的四个角
添加(新向量2(tUnit*tStone.x,tUnit*tStone.y+tUnit));
newUV.Add(新向量2(tUnit*tStone.x+tUnit,tUnit*tStone.y+tUnit));
添加(新向量2(tUnit*tStone.x+tUnit,tUnit*tStone.y));
添加(新向量2(tUnit*tStone.x,tUnit*tStone.y));
//清除网格边界内的任何内容
mesh.Clear();
mesh.vertices=newVertices.ToArray();//将网格顶点设置为我们刚刚创建的新顶点
mesh.triangles=newTriangles.ToArray();
mesh.uv=newUV.ToArray();//将uv应用于网格
mesh.Optimize();//unity做了一些事情
mesh.normals();//
}
void GenSquare(整数x,整数y,矢量2纹理)
{
//定义新正方形的顶点
添加(新向量3(x,y,z));
添加(新向量3(x+1,y,z));
添加(新向量3(x+1,y-1,z));
添加(新向量3(x,y-1,z));
//没有三角形,我们只有空间中的点,没有连接
//这些是顺时针添加的
newTriangles.Add(squareCount*4);//0,0
newTriangles.Add((squareCount*4)+1);//1,0
newTriangles.Add((平方数*4)+3);//-1,0
newTriangles.Add((squareCount*4)+1);//1,0
newTriangles.Add((平方数*4)+2);//-1,1
newTriangles.Add((平方数*4)+3);//-1,0
//定义网格的哪些角用于纹理的四个角
添加(新向量2(tUnit*texture.x,tUnit*texture.y+tUnit));
添加(新向量2(tUnit*texture.x+tUnit,tUnit*texture.y+tUnit));
添加(新向量2(tUnit*texture.x+tUnit,tUnit*texture.y));
添加(新向量2(tUnit*texture.x,tUnit*texture.y));
平方计数++;
}
void GenTerrain()
{
块=新字节[10,10];
for(intpx=0;px=5)
{
块[px,py]=2;
}
否则如果(py<5)
{
块[px,py]=1;
}
}
}
}
void BuildMesh()
{
for(intpx=0;px
GenTerrain();
BuildMesh();
UpdateMesh()

这三个方法调用应该在一个方法中,可能是start或update。计算机认为您正在尝试在这里定义新方法。

GenTerrain();
BuildMesh();
UpdateMesh()


这三个方法调用应该在一个方法中,可能是start或update。计算机认为你在试图定义新方法。

就是这样。我简直疯了。谢谢就是这样。我简直疯了。谢谢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PolygonGenerator : MonoBehaviour
{
// This first list contains every vertex of the mesh that we are going to render
public List<Vector3> newVertices = new List<Vector3>();
// The triangles tell Unity how to build each section of the mesh joining
// the vertices
public List<int> newTriangles = new List<int>();
// The UV list is unimportant right now but it tells Unity how the texture is
// aligned on each polygon
public List<Vector2> newUV = new List<Vector2>();

private float tUnit = 0.25f;
private Vector2 tStone = new Vector2(0, 0);
private Vector2 tGrass = new Vector2(0, 1);

// A mesh is made up of the vertices, triangles and UVs we are going to define,
// after we make them up we'll save them as this mesh
private Mesh mesh;

private int squareCount;
public byte[,] blocks; //0=air 1=rock, 2=grass

// Start is called before the first frame update
void Start()
{
    //gets the mesh of the gameobject
    mesh = GetComponent<MeshFilter>().mesh;

    //gets the x,y, and z values of the gameobject
    //writing x is easier than transform.position.x many times
    float x = transform.position.x;
    float y = transform.position.y;
    float z = transform.position.z;

    // defines what corners of the mesh to use for the four corners of the texture
    newUV.Add(new Vector2(tUnit * tStone.x, tUnit * tStone.y + tUnit));
    newUV.Add(new Vector2(tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
    newUV.Add(new Vector2(tUnit * tStone.x + tUnit, tUnit * tStone.y));
    newUV.Add(new Vector2(tUnit * tStone.x, tUnit * tStone.y));

    //clear anything within the meshes boundries
    mesh.Clear();
    mesh.vertices = newVertices.ToArray(); //set the meshes vertecies to the new ones we just made
    mesh.triangles = newTriangles.ToArray();
    mesh.uv = newUV.ToArray(); // applys uvs to the mesh
    mesh.Optimize(); //unity does some stuff
    mesh.RecalculateNormals(); // 
}

void GenSquare(int x, int y, Vector2 texture)
{
    //defines the vertexes of the new square
    newVertices.Add(new Vector3(x, y, z));
    newVertices.Add(new Vector3(x + 1, y, z));
    newVertices.Add(new Vector3(x + 1, y - 1, z));
    newVertices.Add(new Vector3(x, y - 1, z));

    //without triangels all we have is points in space, no connections
    //these are added clockwise
    newTriangles.Add(squareCount * 4); // 0,0
    newTriangles.Add((squareCount * 4) +1); // 1,0
    newTriangles.Add((squareCount * 4)+3); // -1,0
    newTriangles.Add((squareCount * 4)+1); // 1,0
    newTriangles.Add((squareCount * 4)+2); // -1,1
    newTriangles.Add((squareCount * 4)+3); // -1,0

    // defines what corners of the mesh to use for the four corners of the texture
    newUV.Add(new Vector2(tUnit * texture.x, tUnit * texture.y + tUnit));
    newUV.Add(new Vector2(tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
    newUV.Add(new Vector2(tUnit * texture.x + tUnit, tUnit * texture.y));
    newUV.Add(new Vector2(tUnit * texture.x, tUnit * texture.y));

    squareCount++;
}
void GenTerrain()
{
    blocks = new byte[10, 10];

    for (int px = 0; px < blocks.GetLength(0); px++)
    {
        for (int py = 0; py < blocks.GetLength(1); py++)
        {
            if (py >= 5)
            {
                blocks[px, py] = 2;
            }
            else if (py < 5)
            {
                blocks[px, py] = 1;
            }
        }
    }
}
void BuildMesh()
{
    for (int px = 0; px < blocks.GetLength(0); px++)
    {
        for (int py = 0; py < blocks.GetLength(1); py++)
        {

            if (blocks[px, py] == 1)
            {
                GenSquare(px, py, tStone);
            }
            else if (blocks[px, py] == 2)
            {
                GenSquare(px, py, tGrass);
            }

        }
    }
}

GenTerrain();
BuildMesh();
UpdateMesh();
// Update is called once per frame
void Update()
{
    //clear anything within the meshes boundries
    mesh.Clear();
    mesh.vertices = newVertices.ToArray(); //set the meshes vertecies to the new ones we just made
    mesh.triangles = newTriangles.ToArray();
    mesh.uv = newUV.ToArray(); // applys uvs to the mesh
    mesh.Optimize(); //unity does some stuff
    mesh.RecalculateNormals(); // 

    squareCount = 0;
    newVertices.Clear();
    newTriangles.Clear();
    newUV.Clear();
}
}