简单整理下最近的get到的模块 — — 3d网格地图,这个功能在策略型游戏中应用比较广泛,基本情况下会将地图分割成正方形网格或者六边形网格,下面就针对这两种情况进行实现。当然,鉴于本人能力有限,可能只能达到简单的启发作用,我也是在雨凇momo大神的启发下完成,希望有想法的小伙伴可以提出意见,进行探讨和优化。

实现效果图:

正方形.png

正六边形.png

1.地图网格控制类

public enum GridShapeType

{

///

/// 正方形

///

Square,

///

/// 正六边形

///

RegularHexagon,

}

public class MapGridCtr: MonoBehaviour

{

private const float MAXDIS = 10000001f;

///

/// 网格线是否显示

///

public bool showGrid = true;

///

/// 网格线宽度

///

public float gridLine = 0.1f;

///

/// 网格线颜色

///

public Color gridColor = Color.red;

///

/// 网格线

///

private GameObject[,] m_lines;

///

/// 一个网格的基数

///

public int coefficient = 8;

///

/// 当前地图地形

///

public Terrain m_terrian;

///

/// 当前地图地形行数

///

private int m_arrRow = 0;

///

/// 当前地图地形宽度

///

private int m_arrCol = 0;

///

/// 当前地图vector3数据

///

private Vector3[,] m_array;

///

/// 网片形状

///

public GridShapeType m_meshType;

protected void Start()

{

this.LoadMap();

}

///

/// 加载地图数据

///

public void LoadMap()

{

if (this.m_terrian == null)

{

Debug.Log("地形为空!");

return;

}

if (this.m_meshType == GridShapeType.Square && this.coefficient < 2)

{

Debug.Log("网格基数必须大于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();

}

}

///

/// 显示地图网格

///

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;

}

}

}

///

/// 显示正方形网格

/// coefficient代表边的网格数,最小为2

///

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;

}

}

///

/// 显示正六边形网格

/// 正六边形边长最小为5,coefficient表示倍率

///

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);

}

}

///

/// 创建网格线

///

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.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;

}

}

为什么正六边形的边长是5的倍数的,这是因为网格线是根据地形数据数组生成的,当边长最小为5时,正六边形的六个顶点会在数组中,这也会导致生成正六边形的网格线存在一定限制和漏洞。因为本人项目中网格线只是辅助,实际中并不需要,所以使用了LineRenderer 。

到此,第一部分就结束了,欢迎大家拍砖,提出更好的解决方案。

下一章会实现网格地图的点击选中状态。

unity 地图画格_unity开发之3d网格地图(一)相关推荐

  1. unity 地图画格_unity游戏地形网格地图编辑生成插件Terrain Grid System v10.7

    地形网格系统是一个先进的网格编辑生成器 ,具有强大的地形和二维网格编辑创建功能. 如果你想创建一个战略游戏或RTS游戏,想快速突出显示一些单位下的单元格或显示在控制下的领土, 或者你想让玩家在地形上选 ...

  2. unity 地图画格_Unity2D 四边形与六边形网格地图寻路 [新手]

    毕业几年了, 每天用世界上最好的语言写crud, 有时也挺无聊.最近心血来潮稍微研究了一下Unity, 发现十分有趣, 很适合当作码农的日常休闲娱乐活动. 想象一下,要先做一个游戏,当然得先画个地图, ...

  3. DirectX游戏开发之3D角色动起(下)

    DirectX游戏开发之3D角色动起(下) 直接先上图吧! 动作idle 动作attack 动作walk 动作run 看,多动作的模型搞下来了.原则上只要在此基础上略做修改就可以实现3d游戏的基本制作 ...

  4. iOS开发之3D Touch(快速添加3D Touch功能)

    1. 概述 在支持3D Touch的设备上,用户可以通过对触摸屏施加不同程度的压力来访问其他功能,应用程序可以通过显示上下文菜单(或支持Peek和Pop)来响应,以显示一些可供用户操作的选项或者行为. ...

  5. Unity3D开发之3D按钮的声音播放

    这里我们首先就简易的制作一个非常简单的3D按钮![这里写图片描述](https://img-blog.csdn.net/20170915120955448?watermark/2/text/aHR0c ...

  6. unity android屏幕自适应,Android应用开发之unity打开移动摄像头,并自适应屏幕显示摄像头数据。兼容android和ios...

    本文将带你了解Android应用开发之unity打开移动摄像头,并自适应屏幕显示摄像头数据.兼容android和ios,希望本文对大家学Android有所帮助. 跨平台并自适应显示摄像头数据新建工程并 ...

  7. unity深入研究--开发之C#使用Socket与HTTP连接服务器传输数据包

    unity深入研究--开发之C#使用Socket与HTTP连接服务器传输数据包 转载 2013年01月04日 09:01:15 2243 最近比较忙,有段时间没写博客拉.最近项目中需要使用HTTP与S ...

  8. 二、Unity编辑器开发之ContextMenu

    ContextMenu属性,允许我们在Inspect检视面板对Component组件添加菜单功能. public ContextMenu (string itemName); public Conte ...

  9. android百度地图画圆,Android应用开发之android 百度地图自定义圆,更改默认图标等常用方法...

    本文将带你了解Android应用开发android 百度地图自定义圆,更改默认图标等常用方法,希望本文对大家学Android有所帮助. 总结了一下百度地图常用的方法(前提是集成百度地图环境成功): 1 ...

  10. 开发中的“软”与“硬”:高画质移动游戏开发之道

    摘要:游戏的效果不仅与游戏引擎的渲染相关,与硬件优化也有千丝万缕的联系.一款基于芯片优化的移动游戏界面,甚至可以堪比视频游戏的视觉效果.高通半导体事业部资深经理刘晓光从软硬件两个层面分享了移动游戏开发 ...

最新文章

  1. windows server 2003磁盘管理
  2. 自测之Lesson6:文件I/O
  3. JetsonXavier/Tx2性能测试比对
  4. java cacheutil_Java 常用缓存Cache机制的实现
  5. python 不同模块之间的引用错误问题
  6. C++ 面向对象(一)—— 类(Classes)
  7. js双击事件条件触发_js页面触发chargeRequest事件和Nginx获取日志信息
  8. 数字证书如何写入到ukey_ukey身份认证步骤
  9. 介绍几个比较出名的编程acm题库
  10. 谈谈5G的信道编码方法
  11. word里双横线怎么打_在word中怎么画直线、双直线、虚线
  12. html输入公式得到混合运算结果,excel表格如何用公式计算加减乘除混合运算-excel乘法如何计算,excel函数怎么计算乘法...
  13. QNX Screen---Blit
  14. 八、在创业公司工作的心理历程
  15. WED.文件操作补充及函数
  16. 浙江小学python教材_PPT、H5、Python、大数据……浙江中小学新教材9月投用!
  17. 系统解剖学 | 心血管系统 | 静脉
  18. 30秒集结会议、能开“会中会” IMO班聊助力高效协同办公
  19. 野三坡游记 探访岭南台村
  20. 52.甲乙混战 (15分)

热门文章

  1. 我在京东这一年—张亮
  2. “人在囧途”今年少有的国产好电影
  3. 初中数学计算机图片,初中数学课件背景图片大全.doc
  4. 计算机复制教程,ghost复制c盘到另一个硬盘方法
  5. 【今日CV 计算机视觉论文速览 第150期】Fri, 2 Aug 2019
  6. 自适应数字加密/收藏品NFT市场网站HTML5模版
  7. 摄影焦距和物距的关系_摄影的焦距是多少?
  8. 应用逻辑回归方法对鸢尾花进行分类
  9. conda添加清华镜像源
  10. 计算机怎么看ping,如何查看自己电脑的PING