我正在通过tutorial here。为方便起见,我the package shared through my google drive。
主题GameObject
为HexGridController
,三个已停用的对象:center_fill
,Backdrop
,DiagnosticDisplay
。
除了附加的脚本外,网格控制器是一个空的GameObject:
...和HexGridController脚本:
public class HexGridController : MonoBehaviour, IGridInterface {
public float cellWidth = 50F;
public HexOrientation cellFacing = HexOrientation.Acute;
public int cellCols = 20;
public int cellRows = 14;
public float degOffset = 0.0F;
// ... snipped IGridInterface implementations
// Use this for initialization
private void Start () {
print("Hexagonal Grid Controller started");
print("hex vertices:");
int idx = 0;
foreach(var vert in vertices){
print(string.Format("\t[{0}]: {1}", idx, vert.ToString()));
++idx;
}
}
#region HiddenInInspector: vertices, uv, triangles
[HideInInspector]
public static Vector3[] vertices = new Vector3[]
{
new Vector3(0f, Hexagon.floor, 1f), // north
new Vector3(1f, Hexagon.floor, .5f), // northeast
new Vector3(1f, Hexagon.floor, -.5f), // southeast
new Vector3(0f, Hexagon.floor, -1f), // south
new Vector3(-1f , Hexagon.floor, -.5f), // southwest
new Vector3(-1f, Hexagon.floor, .5f), // northwest
};
[HideInInspector]
public static Vector2[] uv = new Vector2[]
{
new Vector2(0.5F,1), // north
new Vector2(1,0.75F), // northeast
new Vector2(1,0.25F), // southeast
new Vector2(0.5F,0), // south
new Vector2(0,0.25F), // southwest
new Vector2(0,0.75F), // northwest
};
[HideInInspector]
public static int[] triangles = new int[]
{
1,5,0,
1,4,5,
1,2,4,
2,3,4
};
#endregion
}
最后,HexGrid
是GameObject
,附带以下脚本:
public class Hexagon : MonoBehaviour {
private MeshRenderer meshRenderer;
public const float floor = 0;
public HexGridController grid;
public Texture texture;
// Use this for initialization
private void Start () {
print("Hexagon started");
HexGridController.vertices[0] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(1+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(1+0.5)/6))));
HexGridController.vertices[1] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(0+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(0+0.5)/6))));
HexGridController.vertices[2] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(5+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(5+0.5)/6))));
HexGridController.vertices[3] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(4+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(4+0.5)/6))));
HexGridController.vertices[4] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(3+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(3+0.5)/6))));
HexGridController.vertices[5] = new Vector3((grid.cellWidth * Mathf.Cos((float)(2*Mathf.PI*(2+0.5)/6))), Hexagon.floor, (grid.cellWidth * Mathf.Sin((float)(2*Mathf.PI*(2+0.5)/6))));
SetUpMesh();
}
private void SetUpMesh() {
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
meshRenderer = gameObject.GetComponent<MeshRenderer>();
Mesh mesh = new Mesh();
//vertices
mesh.vertices = HexGridController.vertices;
//triangles
mesh.triangles = HexGridController.triangles;
//UV vectors
mesh.uv = HexGridController.uv;
//recalc for lighting
mesh.RecalculateNormals();
//game object's mesh filter
meshFilter.mesh = mesh;
//set to null when not testing
meshRenderer.material.mainTexture = texture;
}
private void Update ()
{
}
private void OnDestory()
{
Object.Destroy(meshRenderer.material);
}
}
注意:
对于它的价值,Hex Facing
是 North 的六边形方向 - 平坦或尖锐(或尖尖)。我打算将最北端的点设为0并顺时针旋转:
0 - North
1 - NorthEast
2 - SouthEast
3 - South
4 - SouthWest
5 - NorthWest
答案 0 :(得分:0)
好的,因为我正在学习Unity,我无法解释为什么教程的API与我发现的正确的差异。我发现有几件事情是不正确的,按照@PavelPájaHalbich的建议,我完成了一个简短的练习,其中我成功地渲染了一个三角形。
现在,我修复的问题和结果......
由于Hexagon提供vertices
和uv
的所有特定数据,因此简化了HexGridController。三角形的数据保留在HexGridController中。
#region HiddenInInspector: vertices, uv, triangles
[HideInInspector]
public static Vector3[] vertices = new Vector3[6];
[HideInInspector]
public static Vector2[] uv = new Vector2[6];
[HideInInspector]
public static int[] triangles = new int[]
{
0,1,2,
2,3,0,
3,4,0,
4,5,0,
};
#endregion
...接下来,Hexagon类在我正确订购vertices
并映射uv
向量时得到了一些改进:
private void Start () {
print("Hexagon started");
// set up vertices
vertices[(int)HexFacing.North] = new Vector3(0F, 1F, floor);
vertices[(int)HexFacing.NorthEeast] = new Vector3(1F, .5F, floor);
vertices[(int)HexFacing.SouthEast] = new Vector3(1F, -.5F, floor);
vertices[(int)HexFacing.South] = new Vector3(0F, -1F, floor);
vertices[(int)HexFacing.SouthWest] = new Vector3(-1F, -.5F, floor);
vertices[(int)HexFacing.NorthWest] = new Vector3(-1F, .5F, floor);
// set up uv
uv[(int)HexFacing.North] = new Vector2(0.5F, 1);
uv[(int)HexFacing.NorthEeast] = new Vector2(1, 0.75F);
uv[(int)HexFacing.SouthEast] = new Vector2(1, 0.25F);
uv[(int)HexFacing.South] = new Vector2(0.5F, 0);
uv[(int)HexFacing.SouthWest] = new Vector2(0, 0.25F);
uv[(int)HexFacing.NorthWest] = new Vector2(0, 0.75F);
SetUpMesh();
}
...最后在SetUpMesh()
方法中进行了一些更正:
private void SetUpMesh() {
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
meshRenderer = gameObject.GetComponent<MeshRenderer>();
Mesh mesh = meshFilter.mesh; // this had been newing a mesh
mesh.Clear(); // this hadn't been called
//vertices
mesh.vertices = vertices;
//triangles
mesh.triangles = HexGridController.triangles;
//UV vectors
mesh.uv = uv;
//recalc for lighting
mesh.RecalculateNormals();
//game object's mesh filter
meshFilter.mesh = mesh;
//set to null when not testing
meshRenderer.material.mainTexture = texture;
}