現在発生している効果は以下の通りです
まず、Excel で各グリッドの高さを入力します。グリッドは 3*3 の位置を占めます (色はマーキングにのみ使用され、効果はありません)。
次に、Excel のデータを txt にコピーし、最後の行の余分な改行を削除すると、次のようになります。
3*3 の高さ属性 (float[,] heights、および独自の座標 Vector2Int posV2) を含むマップ セル タイプ MapCell を作成します。 まず、
txt のデータを読み取り、2 次元配列の mapTextData に格納します。
TextAsset txt = Resources.Load<TextAsset>(filePath);
string fileText = txt.text;
//替换回车
fileText = fileText.Replace("\r\n", "\n");
string[] fileRows = fileText.Split('\n');
int rowCount = fileRows.Length;
int columnCount = fileRows[0].Split(' ').Length;
mapTextData = new string[rowCount, columnCount];
//读取 txt
for (int i = 0; i < rowCount; i++)
{
string[] currentRow = fileRows[i].Split(' ');
for (int j = 0; j < currentRow.Length; j++)
{
mapTextData[i, j] = currentRow[j];
}
}
次に、MapCell[,] の 2 次元配列を作成して、各グリッドの値を記録します。
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
float.TryParse(mapTxtData[i - 1 + row, j - 1 + column], out cellHeights[row, column]);
}
}
最後に、保存されたデータに従ってメッシュが生成されます。頂点の位置情報は、mapCell 内の対応する位置の高さです。各グリッドは 2 つの三角形で構成されているため、メッシュ サーフェスのインデックス配列は最初に現在のデータを記録できます。頂点の数、その後 0 1 2、1 3 2 の接続
vertices.Add(new Vector3(i, mapCell.heights[0, 0] * gridHeight, j) * mapScale);
vertices.Add(new Vector3(i, mapCell.heights[0, 2] * gridHeight, j + 1) * mapScale);
vertices.Add(new Vector3(i + 1, mapCell.heights[2, 0] * gridHeight, j) * mapScale);
vertices.Add(new Vector3(i + 1, mapCell.heights[2, 2] * gridHeight, j + 1) * mapScale);
//记录mesh索引
indices.Add(currentIndices);
indices.Add(currentIndices + 1);
indices.Add(currentIndices + 2);
indices.Add(currentIndices + 1);
indices.Add(currentIndices + 3);
indices.Add(currentIndices + 2);
GameObject が最終的に生成されたら、最初に Cube を生成し、その BoxCollider を削除し、その MeshFilter のメッシュを作成されたメッシュに設定し、Unity に付属する RecalculateNormals を使用してメッシュの法線を再計算します。
完全なプロジェクトは
fbx をエクスポートする必要があります。FBX エクスポーター
ウィンドウ -> パッケージ マネージャーを使用して FBX エクスポーターをインストールできます。