在我的课堂上,我们使用团结来创造游戏。我创建了一个脚本来获取文本字段并根据文本字段生成地图。它生成正确,除了最后一个“瓷砖”。代码看起来像这样。 (所有公共变量都有统一的模型)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MakeFloorPlan : MonoBehaviour {
[TextArea(40,40)]
public string map =
"________\n" +
"________\n" +
"________\n" +
"________\n" +
"________\n" +
"________\n" +
"________\n" +
"________\n";
public GameObject doorV;
public GameObject doorH;
public GameObject floor;
public GameObject wallLeft;
public GameObject wallRight;
public GameObject wallUp;
public GameObject wallDown;
public GameObject wallFloor;
public Vector3 Size = new Vector3 (3, 3, 3);
private int rowLen;
private GameObject[,] allObjects = new GameObject[23, 44];
// Use this for initialization
void Start () {
int row = 0; int col = 0;
for (int index = 0; index < map.Length; index++) {
char c = map [index];
if (c == '\n') {
row++;
col = 0;
} else {
GameObject go;
GameObject whattocreate = null;
Vector3 p = Vector3.zero;
if (c == '•') {
p = new Vector3 (190 + col*Size.x, 0,35 + row*Size.z);
if (col != 0) {
if (map [index - 1] == '•' || map [index - 1] == '_') { whattocreate = wallFloor; Instantiate (wallLeft, p, Quaternion.identity); }
}
if (col != 43) {
if (map [index + 1] == '•' || map [index + 1] == '_') { whattocreate = wallFloor;Instantiate (wallRight, p, Quaternion.identity);}
}
if (row != 0) {
if (map [index - 44] == '•' || map [index - 44] == '_') { whattocreate = wallFloor;Instantiate (wallUp, p, Quaternion.identity);}
}
if (row != 22) {
if (map [index + 44] == '•' || map [index + 44] == '_') { whattocreate = wallFloor;Instantiate (wallDown, p, Quaternion.identity);}
}
}
if (c == ' ') {
p = new Vector3 (190 + col*Size.x, 0,35 + row*Size.z);
whattocreate = floor;
}
if (c == '_') {
p = new Vector3 (190 + col * Size.x, 0, 35 + row * Size.z);
if (map [index - 44] == '•' && map [index + 44] == '•') { whattocreate = doorV; }
if (map [index - 1] == '•' && map [index + 1] == '•') { whattocreate = doorH;}
}
if (whattocreate != null) {
go = Instantiate (whattocreate, p, Quaternion.identity) as GameObject;
allObjects [row, col] = go;
}
col++;
}
}
}
你可以在右上角看到最后的瓷砖搞砸了,我不知道为什么。我的老师也不知道。 (但没有尝试太多)
感谢任何可以帮助我解决这个问题的人:)