试图使 Minecraft(以下教程)代码不起作用
Trying to make Minecraft(following tutorial) code not working
这是我的 Chunk.cs 代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk : MonoBehaviour
{
public MeshRenderer meshRenderer;
public MeshFilter meshFilter;
void Start()
{
int vertexIndex = 0;
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for(int i = 0; i < 6; i++)
{
int triangleIndex = VoxelData.voxelTris[0, 1];
vertices.Add(VoxelData.voxelVerts [triangleIndex]);
triangles.Add(vertexIndex);
uvs.Add(Vector2.zero);
vertexIndex++;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals ();
meshFilter.mesh = mesh;
}
}
这是我的 VoxelData.cs 代码:
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
public static class VoxelData
{
public static readonly Vector3[] voxelVerts = new Vector3[8]
{
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(1.0f, 0.0f, 0.0f),
new Vector3(1.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(1.0f, 0.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(0.0f, 1.0f, 1.0f),
};
public static readonly int[,] voxelTris = new int[1, 6]
{
{3, 7, 2, 2, 7, 6}// Top face
};
}
这是我一直关注的教程:
https://www.youtube.com/watch?v=h66IN1Pndd0&list=PLVsTSlfj0qsWEJ-5eMtXsYp03Y9yF1dEn&index=1
错误可能与第 21 行的函数有关,因为它不像其他添加函数那样是黄色的:
vertices.Add(VoxelData.voxelVerts [triangleIndex]);
提前致谢
只去掉中间的space?
vertices.Add(VoxelData.voxelVerts[triangleIndex]);
因为你没有指出错误,我不得不假设是这样
int triangleIndex = VoxelData.voxelTris[0, 1];
应该是
int triangleIndex = VoxelData.voxelTris[0, i];
如果这不能解决问题,那么我认为您应该更具体地说明错误。
这是我的 Chunk.cs 代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk : MonoBehaviour
{
public MeshRenderer meshRenderer;
public MeshFilter meshFilter;
void Start()
{
int vertexIndex = 0;
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for(int i = 0; i < 6; i++)
{
int triangleIndex = VoxelData.voxelTris[0, 1];
vertices.Add(VoxelData.voxelVerts [triangleIndex]);
triangles.Add(vertexIndex);
uvs.Add(Vector2.zero);
vertexIndex++;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals ();
meshFilter.mesh = mesh;
}
}
这是我的 VoxelData.cs 代码:
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
public static class VoxelData
{
public static readonly Vector3[] voxelVerts = new Vector3[8]
{
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(1.0f, 0.0f, 0.0f),
new Vector3(1.0f, 1.0f, 0.0f),
new Vector3(0.0f, 1.0f, 0.0f),
new Vector3(0.0f, 0.0f, 1.0f),
new Vector3(1.0f, 0.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f),
new Vector3(0.0f, 1.0f, 1.0f),
};
public static readonly int[,] voxelTris = new int[1, 6]
{
{3, 7, 2, 2, 7, 6}// Top face
};
}
这是我一直关注的教程:
https://www.youtube.com/watch?v=h66IN1Pndd0&list=PLVsTSlfj0qsWEJ-5eMtXsYp03Y9yF1dEn&index=1
错误可能与第 21 行的函数有关,因为它不像其他添加函数那样是黄色的:
vertices.Add(VoxelData.voxelVerts [triangleIndex]);
提前致谢
只去掉中间的space?
vertices.Add(VoxelData.voxelVerts[triangleIndex]);
因为你没有指出错误,我不得不假设是这样
int triangleIndex = VoxelData.voxelTris[0, 1];
应该是
int triangleIndex = VoxelData.voxelTris[0, i];
如果这不能解决问题,那么我认为您应该更具体地说明错误。