c# - unity3d 方法必须有返回类型和错误 CS0501
问题描述
我正在关注有关体素网格的教程,但遇到了一个我没想到的错误。这是教程,如果你去看看,我在第二部分。我还认为这可能是一个统一版本的问题,因为这是从 2014 年开始的。我搜索过的错误并没有找到确凿的结果。我一遍又一遍地检查 void 语句,并看到许多重复的 stackoverflow 问题。我也尝试过多次重新启动统一。有关错误的更具体信息:在第 118,119 和 120 行,我收到这两个错误。错误 CS0501:方法必须具有返回类型。错误 CS0501:“PolygonGenerator.PolygonGenerator()”必须声明一个主体,因为它没有标记为抽象、外部或部分。无论如何,我的代码如下:(另外,请更正我的评论并添加更多)
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();
}
}
解决方案
GenTerrain();
BuildMesh();
UpdateMesh();
这三个方法调用应该在一个方法中,可能是start或者update。计算机认为您正在尝试在这里定义新方法。
推荐阅读
- docker - 在 windows server 2016 1607 上使用 Docker linux 容器
- django - Admin中多对多连接表的Django注册视图
- c++ - IncrediBuild - 错误:字符串的第一个字符应为“0”或“1”
- angular - Angular TypeError('readdirp: root argument must be a string.')
- c++ - 在 OpenGL 的“原始组装”阶段会发生什么
- single-sign-on - 使用 cypress 进行 E2E 测试 - Azure AD 单点登录
- nativescript - 如果有较新版本,提示用户更新应用程序
- sql - 如何在 SAP HANA 的动态 sql 中使用表变量?
- reactjs - TypeScript & React useEffect -- 这个表达式是不可调用的。类型“typeof React”没有调用签名 TS2349
- angular - 从用户输入的 URL Angular 中提取字符串