簡(jiǎn)單整理下最近的get到的模塊 — — 3d網(wǎng)格地圖,這個(gè)功能在策略型游戲中應(yīng)用比較廣泛,基本情況下會(huì)將地圖分割成正方形網(wǎng)格或者六邊形網(wǎng)格,下面就針對(duì)這兩種情況進(jìn)行實(shí)現(xiàn)。當(dāng)然,鑒于本人能力有限,可能只能達(dá)到簡(jiǎn)單的啟發(fā)作用,我也是在雨凇momo大神的啟發(fā)下完成,希望有想法的小伙伴可以提出意見(jiàn),進(jìn)行探討和優(yōu)化。
實(shí)現(xiàn)效果圖:

正方形.png

正六邊形.png
1.地圖網(wǎng)格控制類(lèi)
public enum GridShapeType
{
/// <summary>
/// 正方形
/// </summary>
Square,
/// <summary>
/// 正六邊形
/// </summary>
RegularHexagon,
}
public class MapGridCtr: MonoBehaviour
{
private const float MAXDIS = 10000001f;
/// <summary>
/// 網(wǎng)格線是否顯示
/// </summary>
public bool showGrid = true;
/// <summary>
/// 網(wǎng)格線寬度
/// </summary>
public float gridLine = 0.1f;
/// <summary>
/// 網(wǎng)格線顏色
/// </summary>
public Color gridColor = Color.red;
/// <summary>
/// 網(wǎng)格線
/// </summary>
private GameObject[,] m_lines;
/// <summary>
/// 一個(gè)網(wǎng)格的基數(shù)
/// </summary>
public int coefficient = 8;
/// <summary>
/// 當(dāng)前地圖地形
/// </summary>
public Terrain m_terrian;
/// <summary>
/// 當(dāng)前地圖地形行數(shù)
/// </summary>
private int m_arrRow = 0;
/// <summary>
/// 當(dāng)前地圖地形寬度
/// </summary>
private int m_arrCol = 0;
/// <summary>
/// 當(dāng)前地圖vector3數(shù)據(jù)
/// </summary>
private Vector3[,] m_array;
/// <summary>
/// 網(wǎng)片形狀
/// </summary>
public GridShapeType m_meshType;
protected void Start()
{
this.LoadMap();
}
/// <summary>
/// 加載地圖數(shù)據(jù)
/// </summary>
public void LoadMap()
{
if (this.m_terrian == null)
{
Debug.Log("地形為空!");
return;
}
if (this.m_meshType == GridShapeType.Square && this.coefficient < 2)
{
Debug.Log("網(wǎng)格基數(shù)必須大于2!");
return;
}
TerrainData data = m_terrian.terrainData;
int mapz = (int)(data.size.x / data.heightmapScale.x);
int mapx = (int)(data.size.z / data.heightmapScale.z);
this.m_arrRow = Math.Min(data.heightmapWidth, mapz);
this.m_arrCol = Math.Min(data.heightmapHeight, mapx);
float[,] heightPosArray = data.GetHeights(0, 0, this.m_arrRow, this.m_arrCol);
this.m_array = new Vector3[this.m_arrRow, this.m_arrCol];
for (int i = 0; i < this.m_arrRow; ++i)
{
for (int j = 0; j < this.m_arrCol; ++j)
{
this.m_array[i, j] = new Vector3(j * data.heightmapScale.x, heightPosArray[i, j] * data.heightmapScale.y, i * data.heightmapScale.z);
}
}
if (this.showGrid)
{
this.ShowGrid();
}
}
/// <summary>
/// 顯示地圖網(wǎng)格
/// </summary>
private void ShowGrid()
{
switch (m_meshType)
{
case GridShapeType.Square:
{
this.ShowSquareGird();
break;
}
case GridShapeType.RegularHexagon:
{
this.ShowRegularHexagon();
break;
}
default:
{
Debug.LogError("暫不支持此形狀! m_meshType: " + m_meshType);
break;
}
}
}
/// <summary>
/// 顯示正方形網(wǎng)格
/// coefficient代表邊的網(wǎng)格數(shù),最小為2
/// </summary>
private void ShowSquareGird()
{
Vector3[] pos;
int rn = this.m_arrRow / (this.coefficient - 1);
int cn = this.m_arrCol / (this.coefficient - 1);
if (this.m_arrRow % (this.coefficient - 1) > 0)
++rn;
if (this.m_arrCol % (this.coefficient - 1) > 0)
++cn;
this.m_lines = new GameObject[rn, cn];
for (int i = 0; i < this.m_arrRow - 1;)
{
int lastr = i + this.coefficient - 1;
if (lastr >= this.m_arrRow)
{
lastr = this.m_arrRow - 1;
}
for (int j = 0; j < this.m_arrCol - 1;)
{
int lastc = j + this.coefficient - 1;
if (lastc >= this.m_arrCol)
{
lastc = this.m_arrCol - 1;
}
if (lastr < this.m_arrRow - 1 && lastc < this.m_arrCol - 1)
{
pos = new Vector3[this.coefficient * 4];
for (int k = 0; k < this.coefficient; ++k)
{
pos[0 * this.coefficient + k] = this.m_array[i, j + k];
pos[1 * this.coefficient + k] = this.m_array[i + k, lastc];
pos[2 * this.coefficient + k] = this.m_array[lastr, lastc - k];
pos[3 * this.coefficient + k] = this.m_array[lastr - k, j];
}
this.CreatLine(i / (this.coefficient - 1), j / (this.coefficient - 1), pos);
}
else
{
int cr = lastr - i + 1;
int cl = lastc - j + 1;
pos = new Vector3[(cr + cl) * 2];
for (int k = 0; k < cr; ++k)
{
pos[cl + k] = this.m_array[i + k, lastc];
pos[cr + 2 * cl + k] = this.m_array[lastr - k, j];
}
for (int k = 0; k < cl; ++k)
{
pos[k] = this.m_array[i, j + k];
pos[cr + cl + k] = this.m_array[lastr, lastc - k];
}
this.CreatLine(i / (this.coefficient - 1), j / (this.coefficient - 1), pos);
}
j = lastc;
}
i = lastr;
}
}
/// <summary>
/// 顯示正六邊形網(wǎng)格
/// 正六邊形邊長(zhǎng)最小為5,coefficient表示倍率
/// </summary>
private void ShowRegularHexagon()
{
this.coefficient = this.coefficient / 5;
Vector3[] pos_1;
Vector3[] pos_2;
int num_1 = this.m_arrCol / (this.coefficient * (3 + 5)) * (this.coefficient * 5 + 1);
int num_2 = this.m_arrCol % (this.coefficient * (3 + 5));
if (num_2 > 0)
{
if (num_2 < 3 * this.coefficient)
{
num_2 = 1;
}
else
{
num_2 = num_2 - 3 * this.coefficient + 2;
}
}
pos_1 = new Vector3[num_1 + num_2];
pos_2 = new Vector3[num_1 + num_2];
int rn = this.m_arrRow / (this.coefficient * (3 + 5));
this.m_lines = new GameObject[rn, 2];
for (int i = 4 * this.coefficient; i < this.m_arrRow;)
{
int index_1 = 0;
int index_2 = 0;
int r_1 = i - 4 * this.coefficient;
int r_2 = i + 4 * this.coefficient;
bool flag_1 = true;
bool flag_2 = false;
if (r_2 >= this.m_arrRow)
{
flag_1 = false;
}
for (int j = 0; j < this.m_arrCol;)
{
if (j % (this.coefficient * (3 + 5)) == 0)
{
flag_2 = !flag_2;
if (flag_2)
{
pos_1[index_1++] = this.m_array[i, j];
if (flag_1)
{
pos_2[index_2++] = this.m_array[i, j];
}
}
else
{
pos_1[index_1++] = this.m_array[r_1, j];
if (flag_1)
{
pos_2[index_2++] = this.m_array[r_2, j];
}
}
j += 3 * this.coefficient;
}
else
{
if (flag_2)
{
pos_1[index_1++] = this.m_array[r_1, j];
if (flag_1)
{
pos_2[index_2++] = this.m_array[r_2, j];
}
}
else
{
pos_1[index_1++] = this.m_array[i, j];
if (flag_1)
{
pos_2[index_2++] = this.m_array[i, j];
}
}
++j;
}
}
this.CreatLine(i / (2 * 4 * this.coefficient), 0, pos_1);
if (flag_1)
{
this.CreatLine(i / (2 * 4 * this.coefficient), 1, pos_2);
}
i += (4 * this.coefficient * 2);
}
}
/// <summary>
/// 創(chuàng)建網(wǎng)格線
/// </summary>
private void CreatLine(int row, int col, Vector3[] pos)
{
if(this.m_lines[row, col] != null)
{
GameObject.Destroy(this.m_lines[row, col]);
}
this.m_lines[row, col] = new GameObject();
LineRenderer _lineRenderer = this.m_lines[row, col].AddComponent<LineRenderer>();
_lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
_lineRenderer.SetColors(this.gridColor, this.gridColor);
_lineRenderer.SetWidth(this.gridLine, this.gridLine);
_lineRenderer.useWorldSpace = true;
_lineRenderer.SetVertexCount(pos.Length);
for (int i = 0; i < pos.Length; ++i)
{
_lineRenderer.SetPosition(i, pos[i]);
}
this.m_lines[row, col].name = "CreateLine " + row + " " + col;
}
}
為什么正六邊形的邊長(zhǎng)是5的倍數(shù)的,這是因?yàn)榫W(wǎng)格線是根據(jù)地形數(shù)據(jù)數(shù)組生成的,當(dāng)邊長(zhǎng)最小為5時(shí),正六邊形的六個(gè)頂點(diǎn)會(huì)在數(shù)組中,這也會(huì)導(dǎo)致生成正六邊形的網(wǎng)格線存在一定限制和漏洞。因?yàn)楸救隧?xiàng)目中網(wǎng)格線只是輔助,實(shí)際中并不需要,所以使用了LineRenderer 。
到此,第一部分就結(jié)束了,歡迎大家拍磚,提出更好的解決方案。
下一章會(huì)實(shí)現(xiàn)網(wǎng)格地圖的點(diǎn)擊選中狀態(tài)。